: This article mainly introduces the problem of generating objects instead of arrays after the multi-dimensional array json_encode. if you are interested in the PHP Tutorial, refer to it. The json_encode function can encode PHP arrays. the returned value is a json string. Json_encode for multi-dimensional arrays. I always thought it was an array form (for example, {obj1}, {obj2}, {obj3 }}), but today, the interface returns to the client and finds that it has become an object instead of an array (such [{....}, {.....}, {...}]), it is not convenient for client operations. check the code to find that the first unit of the multi-dimensional array is deleted in my previous unset operation, resulting in different returned results. The following code is used:
$ Arr = array (0 => array ('name' => 'Zhang San', 'age' => '123 '), 1 => array ('name' => 'Lily', 'age' => '000000'), 2 => array ('name' => 'Wang 5 ', 'age' => '000000'); $ res = json_encode ($ arr); echo "script console. log ('$ Res'); script ";The result is as follows:
After removing the first index:
$ Arr = array (0 => array ('name' => 'Zhang San', 'age' => '123 '), 1 => array ('name' => 'Lily', 'age' => '000000'), 2 => array ('name' => 'Wang 5 ', 'age' => '000000'); unset ($ arr [0]); $ res = json_encode ($ arr); echo "script console. log ('$ Res'); script ";
The solution is to use the sort function to re-index.
$ Arr = array (0 => array ('name' => 'Zhang San', 'age' => '123 '), 1 => array ('name' => 'Lily', 'age' => '000000'), 2 => array ('name' => 'Wang 5 ', 'age' => '000000'); unset ($ arr [0]); sort ($ arr); $ res = json_encode ($ arr ); echo "script console. log ('$ Res'); script ";
Okay... it seems that there is no value for writing a blog. Just remind yourself of it.
The above introduces the problem of generating objects instead of arrays after the multi-dimensional array json_encode, including some content, and hope to help friends who are interested in PHP tutorials.