从PHP传递一个二维数组转换成JavaScript的最佳方法? [英] Best way to pass a 2d array into JavaScript from PHP?
问题描述
我有一个PHP数组我想在我的JavaScript code使用。我宁愿不做类似< PHP $数组['array2name'] [0]>
为其中的所有元素,因为它的数量是未知。我打算做一个while循环中的一些数据写入到元素,但我似乎无法找到一个简单的方法目前做到这一点。
I have a PHP array I want to use in my JavaScript code. I'd rather not do something like <?PHP $array['array2name'][0]?>
for all of the elements in it as it's number is unknown. I was going to do a while loop to write in some of the data into elements but I cannot seem to find an easy way to do this currently.
我如何通过从PHP二维数组JavaScript中的最简单的方法可能吗?
How do I pass a 2d array from PHP to JavaScript in the easiest way possible?
推荐答案
作为使用json_en code函数的JSON对象。然后,您可以用JavaScript读这很容易,因为它是一个本地JavaScript对象。 http://php.net/manual/en/function.json -en code.php
As a JSON Object using the json_encode function. You can then read this with Javascript easily as it is a native javascript object. http://php.net/manual/en/function.json-encode.php
json_encode($array);
JSON是在JQuery中容易解析的,但对于纯JavaScript在这里看到:
JSON is easily parsable in JQuery, but for pure JavaScript see here:
这篇关于从PHP传递一个二维数组转换成JavaScript的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!