Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is json_decode in PHP guaranteed to preserve ordering of elements when returning an array?

Tags:

json

php

sorting

You can pass a boolean 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 this. When parsing object literals, does this guarantee that the ordering of the items will be preserved? I know JSON object properties aren't ordered, but PHP arrays are. I can't find anywhere in the PHP manual where this is addressed explicitly. It probably pays to err on the side of caution, but I would like to avoid including some kind of "index" sub-property if possible.

like image 744
Marco Avatar asked Jun 18 '09 20:06

Marco


People also ask

Does PHP array preserve order?

PHP array is an ordered map, so, it's a map that keeps the order. array elements just keep the order since they were added that's all.

Does json_decode return array?

json - json_decode does not return array in php - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is the use of json_decode in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

What does json_decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.


4 Answers

Wouldn't it make more sense in this case to use an array when you pass the JSON to PHP. If you don't have any object keys in the JSON (which become associative array keys in PHP), just send it as an array. That way you will be guaranteed they will be in the same order in PHP as in javascript.

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

If you need associative arrays (which is why you are passing the second argument as true), you will have to come up with some way to maintain their order when passing. You will pry have to do some post-processing on the resulting array after you decode it to format it how you want it.

$json = '{[ {"key" : "val"}, {"key" : "val"} ]}';
json_decode($json, true);
like image 62
TJ L Avatar answered Sep 21 '22 07:09

TJ L


Personally, I've never trusted any system to return an exact order unless that order is specifically defined. If you really need an order, then use a dictionary aka 2dimension array and assigned a place value (0,1,2,3...) to each value in the list.

If you apply this rule to everything, you'll never have to worry about the delivery/storage of that array, be it XML, JSON or a database.

Remember, just because something happens to work a certain way, doesn't mean it does so intentionally. It's akin to thinking rows in a database have an order, when in fact they don't unless you use an ORDER BY clause. It's unsafe to think ID 1 always comes before ID 2 in a SELECT.

like image 42
TravisO Avatar answered Sep 25 '22 07:09

TravisO


I've used json_decode() some times, and the results order was kept intact with PHP client apps. But with Python for instance it does not preserve the order.

One way to be reassured is to test it over with multiple examples.

like image 40
rogeriopvl Avatar answered Sep 22 '22 07:09

rogeriopvl


Lacking an explicit statement I'd say, by definition, no explicit order will be preserved.

My primary line of thought it what order, exactly, would this be preserving? The json_decode function takes a string representation of a javascript object literal as it's argument, and then returns either an object or an array. The function's input (object literal as string) has no explicit ordering, which means there's no clear order for the json_decode function to maintain.

like image 32
Alan Storm Avatar answered Sep 24 '22 07:09

Alan Storm