多维数组上的json_encode() - 带有字符串键

时间:2021-11-03 10:48:48

I am creating an very large multidimensional array using PHP. Each object contains Name, ID, ParentID and Children. Children is an array of more objects in the same format.

我正在使用PHP创建一个非常大的多维数组。每个对象都包含Name,ID,ParentID和Children。子元素是一组具有相同格式的更多对象。

It is critical I name the IDs of each object - this helps me put each object under the correct parent. (In the code below, I use 101, 102 etc.)

关键是我命名每个对象的ID - 这有助于我将每个对象放在正确的父对象下。 (在下面的代码中,我使用101,102等)

However, the problem I am having is when I return the array in JSON using json_encode. Each 'Children' array is being printed as an object, not an array - as shown in the JSON code below.

但是,我遇到的问题是当我使用json_encode在JSON中返回数组时。每个'Children'数组都被打印为一个对象,而不是一个数组 - 如下面的JSON代码所示。

As I read on another SO thread here, they "are made as objects because of the inclusion of string keys" - although they are numbers, they are still strings.

当我在这里读到另一个SO线程时,它们“由于包含字符串键而被制作为对象” - 尽管它们是数字,但它们仍然是字符串。

{
"101": {
    "ID": "101",
    "ParentID": "0",
    "Name": "Root One"
    "Children": {
        "102": {
            "ID": "102",
            "ParentID": "101",
            "Name": "Child One"
        },
        "103": {
            "ID": "103",
            "ParentID": "101",
            "Name": "Child Two",
            "Children": {
                "104": {
                    "ID": "104",
                    "ParentID": "103",
                    "Name": "Child Child One"
                }
            }
        },

Does anyone know how to overcome this issue?

有谁知道如何克服这个问题?

Edit: The JSON should look like this (the square brackets are important!):

编辑:JSON应该如下所示(方括号很重要!):

[
{
    "ID": "101",
    "ParentID": "0",
    "Name": "Root One",
    "Children": [
        {
            "ID": "102",
            "ParentID": "101",
            "Name": "Child One",
            "Children": [

4 个解决方案

#1


4  

A JSON array has no explicit indexes, it's just an ordered list. The only JSON data structure that has named keys is an object. The literal should make this quite obvious:

JSON数组没有显式索引,它只是一个有序列表。唯一具有命名键的JSON数据结构是一个对象。文字应该使这一点非常明显:

["foo", "bar", "baz"]

This array has no named indices and there isn't any provision to add any.

此数组没有命名索引,也没有任何添加任何规定。

PHP conflates both lists and key-value stores into one array data type. JSON doesn't.

PHP将列表和键值存储混合为一种数组数据类型。 JSON没有。

#2


2  

This is your object:

这是你的对象:

$parent=new StdClass();
$parent->ID=101;
$parent->ParentID=0;
$parent->Name='Root One';

$child1=new StdClass();
$child1->ID=1011;
$child1->ParentID=$parent->ID;
$child1->Name='Child One';
$parent->Children[]=$child1;


$child1_1=new StdClass();
$child1_1->ID=10111;
$child1_1->ParentID=$child1->ID;
$child1_1->Name='Child One One';
$child1->Children[]=$child1_1;    

This is your JSON convert function:

这是你的JSON转换函数:

echo json_encode($parent,JSON_PRETTY_PRINT);

and this is your object coded into JSON format:

这是您的对象编码为JSON格式:

{
    "ID": 101,
    "ParentID": 0,
    "Name": "Root One",
    "Children": [
        {
            "ID": 1011,
            "ParentID": 101,
            "Name": "Child One",
            "Children": [
                {
                    "ID": 10111,
                    "ParentID": 1011,
                    "Name": "Child One One"
                }
            ]
        }
    ]
}

The answer came later because I started learning PHP later. Anyway, some day, someone might find it useful.

答案来得晚,因为我后来开始学习PHP。无论如何,有一天,有人可能会发现它很有用。

#3


1  

I have now got a working solution which is fast and works well.

我现在有一个快速且运行良好的工作解决方案。

  1. Firstly, as written in SO link from the question;

    首先,如问题中的SO链接所写;

    In JSON, arrays only have numeric keys, whereas objects have string properties. The inclusion of a array key forces the entire outer structure to be an object by necessity.

    在JSON中,数组只有数字键,而对象有字符串属性。包含数组键迫使整个外部结构成为必要的对象。

    In JSON; Curly braces hold objects ({}), Square brackets hold arrays ([]).

    在JSON中;大括号保持对象({}),方括号保持数组([])。

  2. So using a string as a key will result in the json_encode function returning objects, whereas reseting the keys will ensure it creates arrays.

    因此,使用字符串作为键将导致json_encode函数返回对象,而重置键将确保它创建数组。

  3. Therefore, just before I return my JSON encoded string, I run a function to reset all the array keys. The code I found on this SO thread (Reset array keys in multidimensional array) was particularly useful!

    因此,在我返回JSON编码字符串之前,我运行一个函数来重置所有数组键。我在这个SO线程上找到的代码(在多维数组中重置数组键)特别有用!

#4


0  

http://php.net/manual/en/function.json-decode.php

Set 2nd parameter of json_decode to true to atleast get assoc arrays.

将json_decode的第二个参数设置为true以至少获取关联数组。

Aside from that: javascript can't handle non-sequential/non-contiguous array indexes, so as soon as the id's are not sequential, json has no other option then to convert it into "string" indexes.

除此之外:javascript无法处理非顺序/非连续数组索引,因此只要id不是顺序的,json就没有其他选项可以将其转换为“字符串”索引。

#1


4  

A JSON array has no explicit indexes, it's just an ordered list. The only JSON data structure that has named keys is an object. The literal should make this quite obvious:

JSON数组没有显式索引,它只是一个有序列表。唯一具有命名键的JSON数据结构是一个对象。文字应该使这一点非常明显:

["foo", "bar", "baz"]

This array has no named indices and there isn't any provision to add any.

此数组没有命名索引,也没有任何添加任何规定。

PHP conflates both lists and key-value stores into one array data type. JSON doesn't.

PHP将列表和键值存储混合为一种数组数据类型。 JSON没有。

#2


2  

This is your object:

这是你的对象:

$parent=new StdClass();
$parent->ID=101;
$parent->ParentID=0;
$parent->Name='Root One';

$child1=new StdClass();
$child1->ID=1011;
$child1->ParentID=$parent->ID;
$child1->Name='Child One';
$parent->Children[]=$child1;


$child1_1=new StdClass();
$child1_1->ID=10111;
$child1_1->ParentID=$child1->ID;
$child1_1->Name='Child One One';
$child1->Children[]=$child1_1;    

This is your JSON convert function:

这是你的JSON转换函数:

echo json_encode($parent,JSON_PRETTY_PRINT);

and this is your object coded into JSON format:

这是您的对象编码为JSON格式:

{
    "ID": 101,
    "ParentID": 0,
    "Name": "Root One",
    "Children": [
        {
            "ID": 1011,
            "ParentID": 101,
            "Name": "Child One",
            "Children": [
                {
                    "ID": 10111,
                    "ParentID": 1011,
                    "Name": "Child One One"
                }
            ]
        }
    ]
}

The answer came later because I started learning PHP later. Anyway, some day, someone might find it useful.

答案来得晚,因为我后来开始学习PHP。无论如何,有一天,有人可能会发现它很有用。

#3


1  

I have now got a working solution which is fast and works well.

我现在有一个快速且运行良好的工作解决方案。

  1. Firstly, as written in SO link from the question;

    首先,如问题中的SO链接所写;

    In JSON, arrays only have numeric keys, whereas objects have string properties. The inclusion of a array key forces the entire outer structure to be an object by necessity.

    在JSON中,数组只有数字键,而对象有字符串属性。包含数组键迫使整个外部结构成为必要的对象。

    In JSON; Curly braces hold objects ({}), Square brackets hold arrays ([]).

    在JSON中;大括号保持对象({}),方括号保持数组([])。

  2. So using a string as a key will result in the json_encode function returning objects, whereas reseting the keys will ensure it creates arrays.

    因此,使用字符串作为键将导致json_encode函数返回对象,而重置键将确保它创建数组。

  3. Therefore, just before I return my JSON encoded string, I run a function to reset all the array keys. The code I found on this SO thread (Reset array keys in multidimensional array) was particularly useful!

    因此,在我返回JSON编码字符串之前,我运行一个函数来重置所有数组键。我在这个SO线程上找到的代码(在多维数组中重置数组键)特别有用!

#4


0  

http://php.net/manual/en/function.json-decode.php

Set 2nd parameter of json_decode to true to atleast get assoc arrays.

将json_decode的第二个参数设置为true以至少获取关联数组。

Aside from that: javascript can't handle non-sequential/non-contiguous array indexes, so as soon as the id's are not sequential, json has no other option then to convert it into "string" indexes.

除此之外:javascript无法处理非顺序/非连续数组索引,因此只要id不是顺序的,json就没有其他选项可以将其转换为“字符串”索引。