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-formatted array format (Vector) instead of an object format (MAP), then the data is subscript:must be a numeric index,must start from 0,must increase from small to large, in the middle can not jump, the order is not variable.*///Match array$vector=[ A, at, -];EchoJson_encode($vector);//Match array$vector 2=[0= A,1= at,2= -];EchoJson_encode($vector 2);//Does not match composite group, subscript has jump$map=[0= A,1= at,2= -,4= -];EchoJson_encode($map);//Inconsistent with Composite group, subscript order is incorrect$map=[0= A,1= at,3= -,2= -];EchoJson_encode($map);//Inconsistent with Composite group, subscript not starting from 0$map _1=[1=' 111 ',2=' Asdfa '];EchoJson_encode($map _1);//Typical object formats$map _2=[' abc '=1,' de '=2,' fi '=NULL];EchoJson_encode($map _2);/*** Please note that the above variables are named, corresponding to the concepts in hack, vector and map are two types of data sets* If you need to force an "indexed array" into an "object", you can write* Json_encode ((object) $a);* or* Json_encode ($a, json_force_object); * */
What's the solution?
is the class of PHP that encapsulates both vector and map data structures, which are encapsulated and transformed to ensure structural certainty when constructing JSON output structures.
The above describes the object array php JSON array and object understanding, including the object array aspects of the content, I hope the PHP tutorial interested in a friend helpful.