开发者

How to pass a php array of string values to a javascript variable

开发者 https://www.devze.com 2023-03-24 05:09 出处:网络
I need to use php arrays in my javascript code. The arrays are defined in PHP in the same file as the javascript.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消