An in-depth understanding of PHP JSON arrays and object _php instances

Source: Internet
Author: User
Tags data structures php json

In the process of interacting with the backend and client data in PHP, the JSON data is sometimes formatted in a way that is an array and, for a moment, an object, making the client developer feel like a crash.

Therefore, the front and back end 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 universal data structure, and unlike other languages, which define a lot of binding data types as needed, it is difficult for a PHP programmer to give the client a clear idea of what structure is being returned.

It becomes clearly encoded into JSON through the array data of PHP, but the output value is sometimes an array, sometimes an object.

<?php/
*
If you want to generate a JSON-formatted array format (Vector) rather than an object format (MAP), the subscript of the data: must be
a digital index, must
start from 0,
must increase from small to large, The middle can not jump, the order is not changeable.
*
//conforming to array
$vector = [
A,
n];
echo Json_encode ($vector);
Match array
$vector 2 = [
0 =>, 1 =>, 2 =>
];
Echo Json_encode ($vector 2);
does not conform to the array, subscript has jumped
$map = [
0 =>, 1 =>, 2 =>, 4 =>
];
echo Json_encode ($map);
does not conform to the array, subscript the order is not
$map = [
0 =>, 1 =>, 3 =>, 2 =>
];
echo Json_encode ($map);
does not conform to the array, subscript does not start from 0
$map _1 = [
1 => ', 2 => ' Asdfa '
];
Echo Json_encode ($map _1);
Typical object formats
$map _2 = [' abc ' => 1, ' de ' => 2, ' fi ' => null];
Echo Json_encode ($map _2);
/**
* Note the above variable name, corresponding to the concept in hack, vector and map for two types of data sets
* If you need to force the "indexed array" to "object", you can write
* Json_encode ( object) $a);
* or
* Json_encode ($a, json_force_object);
*
*/

What is the solution?

This is a class that encapsulates both vector and map data structures in PHP, and when constructing the JSON output structure, the two types are encapsulated and transformed to ensure structural certainty.

The above is a small set to introduce the PHP JSON array and objects, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.