In my PHP-side I have an array of associative arrays like so:
在我的PHP端,我有一个关联数组数组,如下所示:
Array (
[0] => Array (
[resultId] => 15
[testId] => 4
[accountId] => 35
[score] => 50
[standard_deviation] => 0.5
[answer_time] => 475.67
[created_at] => 2012-09-20 01:45:05
[groupId] => 4
[accountName] => hbbgrewkcx
[testName] => test1),
[1] => Array (
[resultId] => 14
[testId] => 3
[accountId] => 35
[score] => 60
[standard_deviation] => 0.5
[average_answer_time] => 386.1
[created_at] => 2012-09-20 01:44:56
[groupId] => 4
[accountName] => hbbgrewkcx
[testName] => test2)
)
I would like to transfer this array to the javascript side. How can I make the javascript counterpart of this array of associative arrays and access the required values?
我想将此数组传输到javascript端。如何使这个关联数组数组的javascript对应并访问所需的值?
Basically I want the same functionality as foreach would give me in PHP:
基本上我想要与foreach在PHP中给我相同的功能:
foreach($ArrayOfArrays as $array)
{
doSomething $array['testName'];
doSomething $array['created_at'];
}
1 个解决方案
#1
8
All you would have to do essentially is echo out the JavaScript code to the right place on the page.
所有你必须做的就是将JavaScript代码回显到页面上的正确位置。
echo "<script language='text/javascript'>";
echo "var myArr = ".json_encode($phpArray).";";
echo "</script>";
The json_encode()
function, returns a JSON representation of a value. JavaScript handles JSON's very easily so you should be able to access your multidimensional array without problems!
json_encode()函数返回值的JSON表示。 JavaScript非常容易处理JSON,因此您应该能够毫无问题地访问多维数组!
#1
8
All you would have to do essentially is echo out the JavaScript code to the right place on the page.
所有你必须做的就是将JavaScript代码回显到页面上的正确位置。
echo "<script language='text/javascript'>";
echo "var myArr = ".json_encode($phpArray).";";
echo "</script>";
The json_encode()
function, returns a JSON representation of a value. JavaScript handles JSON's very easily so you should be able to access your multidimensional array without problems!
json_encode()函数返回值的JSON表示。 JavaScript非常容易处理JSON,因此您应该能够毫无问题地访问多维数组!