Is json_decode in PHP a guarantee of preserving the order of elements when returning an array?

You can pass a boolean value to json_decode to return an array instead of an object

json_decode('{"foo", "bar", "baz"}', true);  // array(0 => 'foo', 1 => 'bar', 2 => 'baz')

My question is that. When analyzing object literals, does this ensure that the order of the elements is preserved? I know that the properties of the JSON object are not ordered, but arrays of PHP. I cannot find anywhere else in the PHP manual where this is explicitly covered. You should probably beware of caution, but I would like to avoid including any sub-property of the β€œindex” if possible.

+5
source share
4 answers

JSON PHP. JSON ( PHP), . , PHP, javascript.

json_decode('{["foo", "bar", "baz"]}');
json_decode('["foo", "bar", "baz"]'); //I think this would work

( true), - . , , - , , .

$json = '{[ {"key" : "val"}, {"key" : "val"} ]}';
json_decode($json, true);
+4

json_decode() , PHP. Python, , .

- .

+1

- , , . , aka 2dimension array (0,1,2,3...) .

, / , XML, JSON .

, , - , , . , , ORDER BY. , 1 ID 2 SELECT.

+1

Without an explicit expression, I would say that by definition an explicit order will not be maintained.

My first direction is that this order will be preserved? The json_decode function takes a string representation of the literal of a javascript object as an argument, and then returns either an object or an array. The function input (object literal as a string) does not have explicit ordering, which means there is no clear order for the json_decode function to support.

+1
source

All Articles