0

I have an array I pass through json_encode, it wrap array brackets around first array, but wraps object enclosures {} around the second array and numbers the nested array. Its not causing any errors, just wondered why this was happening.

{
"data": {
    "first_array": [
        {
            "meta": {
                "slug": "rank",
                "weight": "100",
                "description": "lorem ipsum",
                "importance": {
                    "value": 2,
                    "max": 5
                }
            },
            "scores": {
                "results": {
                    "slug": "rank",
                    "type": "int",
                    "value": "4"
                }
            }
        },
        {
            "meta": {
                "slug": "rank",
                "weight": "100",
                "description": "lorem ipsum",
                "importance": {
                    "value": 2,
                    "max": 5
                }
            },
            "scores": {
                "results": {
                    "slug": "rank",
                    "type": "int",
                    "value": "4"
                }
            }
        }
    ],
    "second_array": {
        "2": {
            "meta": {
                "slug": "this",
                "weight": "75",
                "description": "lorem ipsum",
                "importance": {
                    "value": 1,
                    "max": 5
                }
            },
            "scores": {
                "results": {
                    "slug": "this",
                    "type": "boolean",
                    "value": 1,
                    "code": 200
                }
            }
        },
        "3": {
            "meta": {
                "slug": "that",
                "weight": "200",
                "description": "lorem ipsum",
                "importance": {
                    "value": 3,
                    "max": 5
                }
            },
            "scores": {
                "results": {
                    "slug": "that",
                    "type": "boolean",
                    "value": 1
                }
            }
        }
    }
}

}

here is a print_r of the array

    Array
(
    [data] => Array
        (
            [first_array] => Array
                (
                    [0] => Array
                        (
                            [meta] => Array
                                (
                                    [slug] => rank
                                    [weight] => 100
                                    [description] => lorem ipsum
                                    [importance] => Array
                                        (
                                            [value] => 2
                                            [max] => 5
                                        )

                                )

                            [scores] => Array
                                (
                                    [results] => Array
                                        (
                                            [slug] => rank
                                            [type] => int
                                            [value] => 4
                                        )

                                )

                        )
                    [1] => Array
                        (
                            [meta] => Array
                                (
                                    [slug] => rank
                                    [weight] => 100
                                    [description] => lorem ipsum
                                    [importance] => Array
                                        (
                                            [value] => 2
                                            [max] => 5
                                        )

                                )

                            [scores] => Array
                                (
                                    [results] => Array
                                        (
                                            [slug] => rank
                                            [type] => int
                                            [value] => 4
                                        )

                                )

                        )

                )

            [second_array] => Array
                (
                    [2] => Array
                        (
                            [meta] => Array
                                (
                                    [slug] => this
                                    [weight] => 100
                                    [description] => lorem ipsum
                                    [importance] => Array
                                        (
                                            [value] => 2
                                            [max] => 5
                                        )

                                )

                            [scores] => Array
                                (
                                    [results] => Array
                                        (
                                            [slug] => this
                                            [type] => boolean
                                            [value] => 1
                                            [code] => 200
                                        )

                                )

                        )
                    [3] => Array
                        (
                            [meta] => Array
                                (
                                    [slug] => that
                                    [weight] => 100
                                    [description] => lorem ipsum
                                    [importance] => Array
                                        (
                                            [value] => 2
                                            [max] => 5
                                        )

                                )

                            [scores] => Array
                                (
                                    [results] => Array
                                        (
                                            [slug] => that
                                            [type] => boolean
                                            [value] => 1
                                        )

                                )

                        )
                )
        )
)

4 Answers 4

3

Because the second is an associative array (has a string as its key). JS arrays can only have numeric indexes.

As of PHP 5.3, you can force json_encode to output objects only with JSON_FORCE_OBJECT

Sign up to request clarification or add additional context in comments.

Comments

0

It may happen because your first_array is really an array starting at index = 0 and is continuous whereas the second_array starts at index = 2, so the function use a index (as string) <=> value association.

B.

Comments

0

The first one starts at key 0, so it looks like a standard numeric array. ie first[0] in javascript will give you the first element.

But the second one starts at 2, that cant be put into a standard javascript array. ie $second[2] in PHP would actully be second[0] in javascript (if it did convert it to an array).

The two number systems arent compatible. Saving as an object, allows arbitary string keys on the data. (more like a associtative array - even with numbers as keys, rather than a numeric array. A javascript numeric array can't skip keys)

You could call

$array['data']['second_array'] = array_values($array['data']['second_array']); 

to discard the current keys of the second array, and get new keys - which will start from 0, and form a normal javascript array when converted.

Comments

0

Just an example for reproduce your status (and try to explain it)

$a = Array("a","b","c","d");
echo json_encode($a); // ["a","b","c","d"]
unset($a[2]);
echo json_encode($a); // {"0":"a","1":"b","3":"d"}

In the first case we have a pure array (numeric). So the json string of this is an array rappresentation ["a","b","c","d"].

However when we unset the third element, the numeric array will be associative array (because the order is not respected yet). Associative array (with numeric index but not sequential index) are rappresented in json like simple object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.