In-depth understanding of php json arrays and objects, and in-depth understanding of json

Source: Internet
Author: User
Tags php json

In-depth understanding of php json arrays and objects, and in-depth understanding of json

In the process of PHP backend and client data interaction, the JSON data is sometimes formatted in an irregular manner. The JSON data is an array and the JSON data is an object, which makes the client developers feel like they want to crash.

Therefore, the frontend and backend personnel must first understand the json_encode function principles of PHP.

The array in PHP is a omnipotent data structure and does not define many binding data types to describe the structure according to the needs of other languages, therefore, it is difficult for PHP programmers to tell the client about the structure of the returned data.

The PHP array data is encoded as json, but the output value is sometimes an array or an object.

<? Php/* if you want to generate a json array format (Vector) instead of an object format (Map), the subscript of the data must be a digital index and start from 0, it must be increased in sequence from small to large, not jumping in the middle, and the order cannot be changed. * /// match the array $ vector = [12, 23, 18]; echo json_encode ($ vector); // match the array $ vector2 = [0 => 12, 1 => 23, 2 => 18]; echo json_encode ($ vector2); // It does not match the array and is marked with jump $ map = [0 => 12, 1 => 23, 2 => 18, 4 => 20]; echo json_encode ($ map); // does not match the array, the subscript order is incorrect. $ map = [0 => 12, 1 => 23, 3 => 18, 2 => 20]; echo json_encode ($ map ); // The subscript does not match the array. The subscript does not start from 0 $ map_1 = [1 => '000000', 2 => 'asdfa ']; echo json_encode ($ map_1 ); // typical object format $ map_2 = ['abc' => 1, 'de' => 2, 'F' => null]; echo json_encode ($ map_2 ); /*** note the above variable name, which corresponds to the concept in Hack, vector and Map are two data set types * If You Need To forcibly convert an "index array" to "object", you can write * json_encode (object) $ a) in this way ); * Or * json_encode ($ a, JSON_FORCE_OBJECT );**/

What is the solution?

It is the class that encapsulates the Vector and Map data structures through PHP. when constructing the json output structure, the two types are encapsulated and converted to ensure the structure's certainty.

The above is a small series of php json arrays and objects for everyone to introduce, I hope to help you, if you have any questions, please leave a message, the small series will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.