Question

i have an simple array:

array
  0 => string 'Kum' (length=3)
  1 => string 'Kumpel' (length=6)

when I encode the array using json_encode(), i get following:

["Kum","Kumpel"] 

My question is, what is the reason to get ["Kum","Kumpel"] instead of { "0" : "Kum", "1" : "Kumpel" }?

Was it helpful?

Solution

"{}" brackets specify an object and "[]" are used for arrays according to JSON specification. Arrays don't have enumeration, if you look at it from memory allocation perspective. It's just data followed by more data, objects from other hand have properties with names and the data is assigned to the properties, therefore to encode such object you must also pass the correct property names. But for array you don't need to specify the indexes, because they always will be 0..n, where n is the length of the array - 1, the only thing that matters is the order of data.

$array = array("a","b","c");
json_encode($array); // ["a","b","c"]
json_encode($array, JSON_FORCE_OBJECT); // {"0":"a", "1":"b","2":"c"}

The reason why JSON_FORCE_OBJECT foces it to use "0,1,2" is because to assign data to obeject you must assign it to a property, since no property names are given by developer (only the data) the encoder uses array indexes as property names, because those are the only names which would make sense.

Note: according to PHP manual the options parameters are only available from PHP 5.3.

For older PHP versions refer to chelmertz's answer for a way to make json_encode to use indexes.

OTHER TIPS

As Gumbo said, on the JS-side it won't matter. To force PHP into it, try this:

$a = new stdClass();
$a->{0} = "Kum";
$a->{1} = "Kumpel";
echo json_encode($a);

Not that usable, I'd stick with the array notation.

Just cast as an object and it will work fine...the JSON_FORCE_OBJECT parameter does exactly the same thing.

json_encode((object)$array);

Don't forget to convert it back into a php array so you can access its values in php:

$array = (object)$array;
$array = (array)$array;

json_encode($array);

Since you’re having a PHP array with just numeric keys, there is no need to use a JavaScript object. But if you need one, try Maiku Mori’s suggestion.

I personally think this is a bug that needs to be fixed in PHP. JSON_FORCE_OBJECT is absolutely not an answer. If you try to do any sort of generic programming you get tripped up constantly. For example, the following is valid PHP:

array("0" => array(0,1,2,3), "1" => array(4,5,6,7));

And should be converted to

{"0": [0,1,2,3], "1": [4,5,6,7]}

Yet PHP expects me to either accept

[[0,1,2,3],[4,5,6,7]]

or

{"0":{"0":1,"1":1,"2":2,"3":3},"1":{"0":4,"1":5,"2":6,"3":7}}

Neither of which are right at all. How can I possibly decode an object like that? What possible reason is there to ever change something that is clearly using strings as indexes? It's like PHP was trying to be clever to help out idiotic people who can't differentiate strings from ints, but in the process messed up anyone legitimately using strings as indexes, regardless of what the value COULD be turned into.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top