Home » Php » php – Why can't I set zero as the first key in the array?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

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
                        )

                )

        )

)

Answer:

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

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