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!