How to convert php array to js array object: first get the [$arr] array; then use the function [json_encode()] to json encode each value of the array; then output a json array; finally use arr accepts an array.
##Related learning recommendations: js video tutorial
How to convert php array to js array object:
First look at the php file. When we get the $arr arrayforeach ($arr as $value) { $json .= json_encode($value) . ','; } echo '[' . substr($json,0,strlen($json) - 1) . ']';
json_encode() is to json encode each value of $arr, and then we want to output a json array, so we add a comma after each compiled value and finally add '[ outside all values ]', this is the format of the json array. Note that because we add a comma after each value is json encoded, this will result in an extra comma when all values are merged into the array, so we have to use substr() The function removes its last comma!
var json = JSON.parse(arr);
If you want to learn more about programming, please pay attention to the php training column!
The above is the detailed content of How to convert php array to js array object. For more information, please follow other related articles on the PHP Chinese website!