I need to use php arrays in my javascript code. The arrays are defined in PHP in the same file as the javascript.
I have tried the following two ways:
var array_name = new Array('<?php echo implode("','", $php_array);?>');
and
var array_name = <?php echo json_encode($php_array);?>;
This works great if my php arrays are integers. However, when I try to do either when the array is a list of strings, then it does not开发者_运维百科 work.
Any suggestions on what other options I have?
Any help appreciated.
put that json string into quotes (single quotes!)
var array_name = '<?php echo json_encode($php_array);?>';
enjooy
精彩评论