0

I have a multidimensional array, i wish to extract each value from this array. The array is stored in $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

but wasn't able to do so.

can anyone tell how i can fetch all the data especially C22

4
  • json_decode and then loop it Commented Feb 11, 2015 at 8:01
  • @Feroz Akbar i don't know much about json, can u plz tell me how i can decode it Commented Feb 11, 2015 at 8:02
  • $total = $data['categories'][0]['category_id']; Commented Feb 11, 2015 at 8:03
  • 1
    every time you see a { it means an object and every [ means an array. So you were missing the array part [0]. To access each element you can use a recursive function that drills down to each element. Commented Feb 11, 2015 at 8:05

3 Answers 3

2

You have to first use 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:

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

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

2 Comments

thanks i was able to convert it to array. but in the loop i got an error Warning: Invalid argument supplied for foreach(). an u plz look into it
Your json is not valid, I have corrected it and added it to the answer
0

The data is not in PHP array, its a JSON array. You have to decode it, by using json_decode() function.

Comments

-1

That's JSON, not php multidimensional array. You can use json_decode function to read through it.

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.