Convert php array to js array. how does js receive PHP array, json usage, and jsjson. Convert php array to js array. how does js receive PHP array and json usage? jsjson first downloads the following file (this is a piece of code written by someone else to parse json ), then introduce the php array to the js array. how does js receive the PHP array, json usage, and jsjson
First download the following file (this is a code written by someone else to parse json), and then introduce this file!
Http://pan.baidu.com/s/1dD8qVr7
Now, when we need to use ajax to interact with the background, how can we transmit php arrays to js files and identify them by js?
First Read the php file. when we get the $ arr array
foreach ($arr as $value) { $json .= json_encode($value) . ',';} echo '[' . substr($json,0,strlen($json) - 1) . ']';
Json_encode () is to encode every value of $ arr in json format, and then we want to output a json Array, therefore, we add a comma after each compiled value and add '[]' to all values. this is the format of the json Array, note that a comma is added after each value is encoded in json format, which leads to an additional comma when all values are merged into the array. substr () is used for all values () remove the last comma from the function!
Then let's look at the js file.
After we use arr to receive the json Array transmitted to the PHP file
var json = JSON.parse(arr);
JSON is an object defined in the file we downloaded. We use its parse method to convert the json Array into an array of js! This variable json receives a js array, so it cannot be printed directly. you can traverse this json Array or json [0] to output it!
In fact, the idea of converting a php array into a js array is to use the intermediate volume of json! Of course, you can also use php and js to convert arrays. there are more than one method!
Reprinted please indicate the source: http://www.ly89.cn/detailB/44.html
Pipeline first downloads the following file (this is a code written by someone else to parse json), and then introduces this...