简体   繁体   English

从数组中的数组中获取值

[英]fetch values from array within an array

I have a multidimensional array, i wish to extract each value from this array. 我有一个多维数组,我希望从此数组中提取每个值。 The array is stored in $data. 数组存储在$ data中。

{"success":true,"categories":
    [
        {"
            category_id":"C1",
            "parent_id":"P1",
            "name":"N1",
            "categories":
                [
                    {
                        "category_id":"C11",
                        "parent_id":"P11",
                        "name":"N11",
                    },
                    {
                        "category_id":"C12",
                        "parent_id":"P12",
                        "name":"N12",

                    },
                ],
            "status":"1"
        },

        {
            category_id":"C2",
            "parent_id":"P2",
            "name":"N2",
            "categories":
                [
                    {
                        "category_id":"C21",
                        "parent_id":"P21",
                        "name":"N21",
                            [
                                {
                                    "category_id":"C22",
                                    "parent_id":"P23",
                                    "name":"N24",
                                }
                            ],
                        "status":"2"
                    }
                ],
            "status":"3"
        },
    ]
}

I tried using 我尝试使用

$total = $data['categories']['category_id'];

to fetch value C11 取值C11

but wasn't able to do so. 但无法做到。

can anyone tell how i can fetch all the data especially C22 谁能告诉我如何获取所有数据,尤其是C22

You have to first use json_decode . 您必须首先使用json_decode

$array = json_decode($data, true);

Then you can access the array as you have stated. 然后,您可以按照说明访问阵列。 Or loop throught the categories: 或循环浏览以下类别:

if (!empty($array)) {
    foreach ($array['categories'] as $category) {
        echo $category['id'];
    }
}

You may have to do this recursively to loop through the categories within the categories. 您可能必须递归执行此操作,才能在类别中遍历类别。 But it depends completely what you want to achieve. 但这完全取决于您要实现的目标。 A nested loop could do the job if it is always just one level deep. 如果嵌套循环始终只有一个深度,则可以完成此工作。

EDIT 编辑

The JSON you have provided is not quite right, I have given a corrected one below: 您提供的JSON不太正确,我在下面提供了一个更正的JSON:

{
"success": true,
"categories": [
    {
        "category_id": "C1",
        "parent_id": "P1",
        "name": "N1",
        "categories": [
            {
                "category_id": "C11",
                "parent_id": "P11",
                "name": "N11"
            },
            {
                "category_id": "C12",
                "parent_id": "P12",
                "name": "N12"
            }
        ],
        "status": "1"
    },
    {
        "category_id": "C2",
        "parent_id": "P2",
        "name": "N2",
        "categories": [
            {
                "category_id": "C21",
                "parent_id": "P21",
                "name": "N21",
                "categories": [
                    {
                        "category_id": "C22",
                        "parent_id": "P23",
                        "name": "N24"
                    }
                ],
                "status": "2"
            }
        ],
        "status": "3"
    }
]
}

There were a few trailing commas and missing quote marks. 有一些结尾的逗号和引号丢失。

The data is not in PHP array, its a JSON array. 数据不在PHP数组中,而是JSON数组。 You have to decode it, by using json_decode() function. 您必须使用json_decode()函数对其进行解码。

That's JSON, not php multidimensional array. 那是JSON,而不是php多维数组。 You can use json_decode function to read through it. 您可以使用json_decode函数来读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM