Why can't I set zero as the first key in the array?

arrays, json, php

Solution

You want json_encode($a, JSON_FORCE_OBJECT). Unfortunately, it's only added in 5.3.

Problem

The result of this code: ``` for($i = 0; $i <= 7; $i++){ $eachone[] = array ('a' => '1', 'b' => '2', 'c' => '3'); $a[] = array($i => $eachone); unset($eachone); } $json_string = json_encode($a); echo $json_string; ``` is: ``` [ [ [ { "a": "1", "b": "2", "c": "3" } ] ], { "1": [ { "a": "1", "b": "2", "c": "3" } ] }, { "2": [ { "a": "1", "b": "2", "c": "3" } ] }, { "3": [ { "a": "1", "b": "2", "c": "3" } ] }, { "4": [ { "a": "1", "b": "2", "c": "3" } ] }, { "5": [ { "a": "1", "b": "2", "c": "3" } ] }, { "6": [ { "a": "1", "b": "2", "c": "3" } ] }, { "7": [ { "a": "1", "b": "2", "c": "3" } ] } ] ``` Can you notice how it's skipping the first number, which is zero? The question is: Why?

Original source