0

我有一个通过 json_encode 传递的数组,它将数组括号包裹在第一个数组周围,但将对象附件 {} 包裹在第二个数组周围并为嵌套数组编号。它没有引起任何错误,只是想知道为什么会这样。

{
"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
                }
            }
        }
    }
}

}

这是数组的 print_r

    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

4 回答 4

3

因为第二个是关联数组(以字符串为键)。JS 数组只能有数字索引。

自 PHP 5.3 起,您只能使用JSON_FORCE_OBJECTjson_encode强制输出对象

于 2013-03-07T16:25:58.637 回答
0

这可能会发生,因为您的 first_array 实际上是一个从 index = 0 开始并且是连续的数组,而 second_array 从 index = 2 开始,所以该函数使用索引(作为字符串)<=> 值关联。

B.

于 2013-03-07T16:25:43.857 回答
0

第一个从键 0 开始,所以它看起来像一个标准的数值数组。即first[0]在javascript中会给你第一个元素。

但是第二个从 2 开始,不能放入标准的 javascript 数组中。即 PHP 中的 $second[2] 实际上会second[0]在 javascript 中(如果它确实将其转换为数组)。

这两个数字系统不兼容。保存为对象,允许在数据上使用任意字符串键。(更像是一个关联数组 - 即使将数字作为键,而不是数字数组。javascript 数字数组不能跳过键)

你可以打电话

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

丢弃第二个数组的当前键,并获取新的键 - 这将从 0 开始,并在转换时形成一个普通的 javascript 数组。

于 2013-03-07T16:26:48.000 回答
0

只是重现您的状态的示例(并尝试解释它)

$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"}

在第一种情况下,我们有一个纯数组(数字)。所以 this 的 json 字符串是一个数组 rappresentation ["a","b","c","d"]

但是,当我们取消设置第三个元素时,数字数组将是关联数组(因为尚未遵守顺序)。关联数组(具有数字索引但没有顺序索引)像简单对象一样以 json 表示。

于 2013-03-07T16:37:09.683 回答