How do I pass a PHP array or object to javascript


After searching the web for some methods, I finally adopted the method of json string.

Suppose there is a PHP array $arr, the code is as follows:


<script> //The default script in html5 is javascript, so there is no need to specify script language specifically
var arr_js = <?php echo json_encode($arr)?>;
</script>