This question already has an answer here:
这个问题已经有了答案:
- Convert php array to Javascript 15 answers
- 将php数组转换为Javascript 15答案
I am unable to figure out how to assign PHP
array to jQuery
array?.
我不知道如何将PHP数组分配给jQuery数组?
I want to do something like the following:
我想做如下事情:
var jQueryArray = <?php $phpArray; ?>;
Can anyone tell me how I can do this?
有人能告诉我怎么做吗?
7 个解决方案
#1
33
Use json encode.
使用json编码。
json_encode — Returns the JSON representation of a value
json_encode——返回值的JSON表示
Example:
例子:
var arrayFromPHP = <?php echo json_encode($arr); ?>;
#2
7
You need to use json_encode
您需要使用json_encode。
var jQueryArray = <?php echo json_encode($phpArray); ?>;
#3
7
You could use the json_encode
function:
您可以使用json_encode函数:
var jQueryArray = <?php echo json_encode($phpArray); ?>;
#4
3
You can use json_encode
您可以使用json_encode
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
#5
3
Don't forget that PHP json_encode will only work on UTF8 encoded text ...
不要忘记PHP json_encode将只用于UTF8编码的文本……
$jsonString = json_encode(array_map(utf8_encode, $rawArray));
would be a more universal solution I think, but I'm a bit tired so 'scuse any coding gaffs ...
我认为这将是一个更普遍的解决方案,但是我有点累了,所以,我要把所有的代码错误都删掉……
#6
2
It's not going to be a JQuery array, it's a javascript array (just to clarify since it's sounds like you're probably a noob). Set your JS array to this:
它不是JQuery数组,而是javascript数组(只是澄清一下,因为听起来你可能是个noob)。将你的JS数组设置为:
<?php echo json_encode($phpArray);?>
See php json_encode docs: http://php.net/manual/en/function.json-encode.php
参见php json_encode文档:http://php.net/manual/en/function.json-encode.php
#7
-1
You can use Json or use foreach in HTML file
可以在HTML文件中使用Json或foreach
<?php foreach($phpArray as $key => $val): ?>
jQueryArray[<?php echo $key; ?>] = <?php echo $val; ?>
<?php endforeach; ?>
#1
33
Use json encode.
使用json编码。
json_encode — Returns the JSON representation of a value
json_encode——返回值的JSON表示
Example:
例子:
var arrayFromPHP = <?php echo json_encode($arr); ?>;
#2
7
You need to use json_encode
您需要使用json_encode。
var jQueryArray = <?php echo json_encode($phpArray); ?>;
#3
7
You could use the json_encode
function:
您可以使用json_encode函数:
var jQueryArray = <?php echo json_encode($phpArray); ?>;
#4
3
You can use json_encode
您可以使用json_encode
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
#5
3
Don't forget that PHP json_encode will only work on UTF8 encoded text ...
不要忘记PHP json_encode将只用于UTF8编码的文本……
$jsonString = json_encode(array_map(utf8_encode, $rawArray));
would be a more universal solution I think, but I'm a bit tired so 'scuse any coding gaffs ...
我认为这将是一个更普遍的解决方案,但是我有点累了,所以,我要把所有的代码错误都删掉……
#6
2
It's not going to be a JQuery array, it's a javascript array (just to clarify since it's sounds like you're probably a noob). Set your JS array to this:
它不是JQuery数组,而是javascript数组(只是澄清一下,因为听起来你可能是个noob)。将你的JS数组设置为:
<?php echo json_encode($phpArray);?>
See php json_encode docs: http://php.net/manual/en/function.json-encode.php
参见php json_encode文档:http://php.net/manual/en/function.json-encode.php
#7
-1
You can use Json or use foreach in HTML file
可以在HTML文件中使用Json或foreach
<?php foreach($phpArray as $key => $val): ?>
jQueryArray[<?php echo $key; ?>] = <?php echo $val; ?>
<?php endforeach; ?>