The Json_encode function can encode an array of PHP, and the return value is a JSON-formatted string.
For multidimensional arrays to Json_encode, I always thought it was a return array form (like {{obj1}, {obj2}, {obj3}}), but today the interface is returned to the client discovery as an object rather than an array (in the form of [{... ...},{.....},{...}] ), not convenient for client operation, check the code only to find that I previously a unset operation to the multidimensional array of the first unit to delete, resulting in a different return results.
Look at the code below:
$arr = Array (
<span style= "White-space:pre" > </span>0=>array (' name ' => ' John ', ' age ' => ') ),
<span style= "White-space:pre" > </span>1=>array (' name ' => ' Dick ', ' age ' => ' 111 '),
<span style= "White-space:pre" > </span>2=>array (' name ' => ' Harry ', ' Age ' => ' 233 ')
);
$res = Json_encode ($arr);
echo "<script>console.log (' $res ');</script>";
The results are as follows:
Then look after the first index is removed:
$arr = Array (
0=>array (' name ' => ' John ', ' Age ' => ' "),
1=>array (' name ' => ' Dick ', ' age ' => ' 111 ') ,
2=>array (' name ' => ' Harry ', ' Age ' => ' 233 ')
);
unset ($arr [0]);
$res = Json_encode ($arr);
echo "<script>console.log (' $res ');</script>";
The solution is to use the Sort function to index again.
$arr = Array (
0=>array (' name ' => ' John ', ' Age ' => ' "),
1=>array (' name ' => ' Dick ', ' age ' => ' 111 ') ,
2=>array (' name ' => ' Harry ', ' Age ' => ' 233 ')
);
unset ($arr [0]);
Sort ($arr);
$res = Json_encode ($arr);
echo "<script>console.log (' $res ');</script>";