How do I pass a PHP array or object to javascript

  • 2020-03-30 02:24:47
  • OfStack

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)?>; 

Related articles: