Multi-dimensional array json_encode produces objects instead of arrays

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.