如何字符串值的PHP数组传递给一个JavaScript变量 [英] How to pass a php array of string values to a javascript variable
本文介绍了如何字符串值的PHP数组传递给一个JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在我的javascript code使用PHP数组。该数组在PHP中定义的同一个文件中的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);?>');
和
var array_name = <?php echo json_encode($php_array);?>;
如果我的PHP数组是整数,这个伟大的工程。然而,当我尝试做既可以当数组是一个字符串列表,那么它不工作。
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?
任何帮助AP preciated。
Any help appreciated.
推荐答案
把那个JSON字符串引号(单引号!)
put that json string into quotes (single quotes!)
var array_name = '<?php echo json_encode($php_array);?>';
enjooy
enjooy
这篇关于如何字符串值的PHP数组传递给一个JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文