Recently in the front-end docking, encountered a problem, the front-end required to return the data format is left, but I returned through the Json_encode data format is the right Note: The data format from "[]" (array) into "{}" (object)
Google, found that: JavaScript does not support associative arrays, PHP indexed array into JSON is an array ([]), the PHP associative array to JSON is an object ({}), so if it is an associative array, you need to do some processing of arrays
Arrays in PHP: indexed arrays and associative arrays
PHP supports two arrays, one is an indexed array (indexed array) that only holds "value", and the other is an associative array (associative array) that holds "name value pairs" (name/value).
Because JavaScript does not support associative arrays, json_encode () only converts an indexed array (indexed array) into an array format, and the associative array (associative array) to the object format.
For example:
// 1. Indexed array $arr Array (' One ', ' both ', ' three '); echo json_encode ($arr); // return Value:
// 2. Associative Arrays $arr Array (' 1 ' = ' one ', ' 2 ' = '-'-', ' 3 ' = ' three '); echo json_encode ($arr); // return Result:
Therefore, if you need to achieve the result of figure one, we need to process the associative array in our array (to an indexed array)
Recommend a function:
Array_values (): Returns all values of the array (non-key names)
$a=array("Name" = "Bill", "age" = "all", "country" = "USA"); Print_r (array_values($a)); // return Results Array ([0] = Bill [1] [2] = USA)
Finally, after the Json_encode processing, it will reach the result of Figure I.
Above
An analysis of arrays in PHP and arrays in JS