Why does the order change when json_encode is output to the front end after the php array is sorted? This is the sorted array written to the file using the file_put_contents function. The order is correct. This is the data output to the foreground using echojson_encode (), and the order has changed. Why...
Why does the order change when json_encode is output to the front end after the php array is sorted?
This is the sorted array written to the file using the file_put_contents function. The order is correct.
This is the data output to the foreground using echo json_encode (), and the order has changed.
Why? I cannot figure it out. who can answer it.
Reply content:
Why does the order change when json_encode is output to the front end after the php array is sorted?
This is the sorted array written to the file using the file_put_contents function. The order is correct.
This is the data output to the foreground using echo json_encode (), and the order has changed.
Why? I cannot figure it out. who can answer it.
Although php arrays are ordered, objects in javascript or json standards do not guarantee the order, any changes to keys in the json Library comply with the standard behavior, the processing of key order (retained/sorted/out-of-order) in any specific implementation should not be dependent
To ensure the order, we recommend that you use "array of object" to modify the data structure.
{ cnt: [ {id: "12", value:"1"}, {id: "03", value:"1"}, ...
json
Is unordered
You have encountered a similar problem before. you can output the json_encode value in the form of a string. the front end can get the string and convert it by yourself.
In Chrome, this is indeed the case.
Even if you outputkey => value
Structure,json_encode
The order is normal.
However, Chrome will sort, even if it is a "string KEY" or "number KEY", in a forward order.
So the final solution is
array_value