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