Take the 2-minute tour ×

When doing a json_encode a multidimensional array in PHP, I'm noticing a different output simply by naming one of the arrays, as opposed to not naming them. For Example:

$arrytest = array(array('a'=>1, 'b'=>2),array('c'=>3),array('d'=>4));

gives a single array of multiple json objects


whereas simply assigning a name to the middle array

$arrytest = array(array('a'=>1, 'b'=>2),"secondarray"=>array('c'=>3),array('d'=>4));

creates a single json object with multiple json objects inside


why would the 1st option not return the same reasults as the 2nd execpt with "1" in place of "secondarray"

share|improve this question
json.org –  squint May 28 '12 at 2:30
Felix Kling, why did you remove my json-encode tag? I'm not saying you shouldn't have, but rather I want to make sure I'm folllowing the correct procedure for tagging, being that json-encode is in my code, I though it would apply as a proper tag. –  dangel May 28 '12 at 2:45

3 Answers 3

up vote 12 down vote accepted

In JSON, arrays [] only every have numeric keys, whereas objects {} have string properties. The inclusion of a array key in your second example forces the entire outer structure to be an object by necessity. The inner objects of both examples are made as objects because of the inclusion of string keys a,b,c,d.

If you were to use the JSON_FORCE_OBJECT option on the first example, you should get back a similar structure to the second, with the outer structure an object rather than an array. Without specifying that you want it as an object, the absence of string keys in the outer array causes PHP to assume it is to be encoded as the equivalent array structure in JSON.


// Force the outer structure into an object rather than array
echo json_encode($arrytest , JSON_FORCE_OBJECT);

// {"0":{"a":1,"b":2},"1":{"c":3},"2":{"d":4}}
share|improve this answer
Then why call it JAVASCRIPT OBJECT NOTATION, if it had nothing to do with Javascript. –  Brendan May 28 '12 at 2:40
Makes sense now, thanks for that explanation I think that will get me headed in the right direction. –  dangel May 28 '12 at 2:42
@Brendan Why does it have nothing to do with Javascript? –  deceze May 28 '12 at 2:43
@deceze An earlier comment providing context for this was removed. –  Michael Berkowski May 28 '12 at 2:47
Yeah, now it looks stupid that I had that there. For reference, someone said JSON had nothing to do with Javascript. –  Brendan May 28 '12 at 2:52

Arrays with continuous numerical keys are encoded as JSON arrays. That's just how it is. Why? Because it makes sense.

Since the keys can be expressed implicitly through the array encoding, there is no reason to explicitly encoded them as object keys.

See all the examples in the json_encode documentation.

share|improve this answer
I'm not saying they shouldn't be, that's why I was asking the question. –  dangel May 28 '12 at 2:37

At the first option you only have numeric indexes (0, 1 and 2). Although they are not explicitly declared, php automatically creates them.

At the second option, you declare one of the indexes as an string and this makes PHP internally transform all indexes to string.

When you json encode the first array, it's not necessary to show the integers in the generated json string because any decoder should be able to "guess" that they are 0, 1 and 2.

But in the second array, this is necessary, as the decoder must know the key value in your array.

It's pretty simple. No indexes declared in array? Them they are 0, 1, 2, 3 and so on.

share|improve this answer

Your Answer


