PHP JSON array and object understanding

Source: Internet
Author: User
Tags php json

In the process of interaction between the PHP backend and client data, the JSON data is sometimes formatted in the form of an array, and in a moment the object makes the client developer feel like crashing.

Therefore, the front and rear related personnel first to PHP's Json_encode function principle has the necessary understanding is the most important link.

The array in PHP is a versatile data structure, and unlike other languages that define a number of constrained data types as needed, it is difficult for PHP programmers to tell the client what structure of data is returned.

It becomes clear that it is encoded into JSON through the PHP array data, but the output value is sometimes an array, sometimes an object case.

/** If you want to generate a JSON format array (vector) instead of an object format (map), then the array subscript must be an indexed array * * Must start from 0 * must be increased from small to large, the middle does not intentionally jump, the order can not be changed **/$vector 0= [    1, 2, 3, 4, 5];EchoJson_encode ($vector 0)." <br/> ";$vector 1= [0 = 0, 1 = 1, 2 = 2];EchoJson_encode ($vector 1)." <br/> " ;$map 0= [0 = 0, 3 = 1, 5 = 2];EchoJson_encode ($map 0)." <br/> " ;$map 1= [1 = 1, 2 = 2];EchoJson_encode ($map 1)." <br/> " ;$map 2= [' a ' = = ' A ', ' B ' = ' B ', ' c ' = ' C '];EchoJson_encode ($map 2)." <br/> ";

In fact, JavaScript does not have an associative array in the browser, so the Json_encode () function must convert the associative array into an object. (I guess: the front-end array is basically continuous, so the group cannot be converted into groups).

If you want to convert an array to a JSON object, there are two types:

$vector 0 = [    1, 2, 3, 4, 5]; echo json_encode ($vector 0, json_force_object). " <br/> "; Echo Json_encode ((object)$vector 0). " <br/> ";

To convert an array to a JSON object:

// JSON = array $json _ob = ' {' A ': ' A ', ' B ': ' B ', ' C ': ' C '} '; Var_dump (Json_decode ($json _ob));//is to generate a PHP object: Var_dump (Json_decode ($json _ob,true));//Add a parameter true to convert to array

PHP JSON array and object understanding

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.