tags:

views:

102

answers:

3

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?

A: 

You didnt indexed your array

for($i = 0; $i <= 7; $i++)
{

    $eachone[$i] = array ('a' => '1', 'b' => '2', 'c' => '3');

$a[$i] = array($i => $eachone);

unset($eachone);

}

$json_string = json_encode($a);
echo $json_string;

*EDIT:*On My Laptop if i do a print_r($a); i get this array: Which sets zero

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

    [1] => Array
        (
            [1] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

    [2] => Array
        (
            [2] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

    [3] => Array
        (
            [3] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

    [4] => Array
        (
            [4] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

    [5] => Array
        (
            [5] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

    [6] => Array
        (
            [6] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

    [7] => Array
        (
            [7] => Array
                (
                    [0] => Array
                        (
                            [a] => 1
                            [b] => 2
                            [c] => 3
                        )

                )

        )

)
streetparade
Technically should not be necessary. The [] syntax is supposed to automatically increment the index (starting at 0, obviously). Yours would not work as desired if $a already has items and his for loop is simply adding to them.
Brock Batsell
It's still skipping '0'.
caioiglesias
+2  A: 

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

bluej100
Also, here's a simpler test case:php -r 'var_dump(json_encode(array(array(0 => 0), array(1 => 1))));'string(13) "[[0],{"1":1}]"
bluej100
I can't even test it since Rackspace Cloud Sites is stuck @ 5.2.6. I'll try working around this json_encode issue.
caioiglesias
Good, I managed to use json_encode properly from an object instead of an array. The problem is it is outputting strings everywhere and not integers like I would need in some cases, but I'll take care of that later. Thanks!
caioiglesias
A: 

It's a hack but you could prepend a dummy element.

$a = array('dummy'=>1);
for($i = 0; $i <= 7; $i++) {
  ...
VolkerK