views:

319

answers:

4

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.

+2  A: 

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);
tj111
I can't find any confirmation of the sorting so I'm gonna have to ensure they are sorted myself. Thanks.
Marco
A: 

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.

rogeriopvl
Great you test and it works now, but you never know if an update will alter this order, better safe than sorry, set an order if you need an order.
TravisO
Yes you're absolutely right, but he was mentioning that the JSON has no keys, so there's no much choice left, but rely on the PHP way to deal with, unless he uses associative arrays.
rogeriopvl
A: 

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.

TravisO
A: 

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.

Alan Storm