开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜