0

I am pulling data from a multidimensional array. While using foreach loop, I am getting confused and can't pull out data from my array. Here is my array named as $result_array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [category_id] => 19
                    [category_name] => Food
                    [publication_status] => 1
                    [deletion_status] => 1
                )

            [1] => Array
                (
                    [category_id] => 16
                    [category_name] => Gourmet
                    [publication_status] => 1
                    [deletion_status] => 1
                )

            [2] => Array
                (
                    [category_id] => 17
                    [category_name] => Islamic Product
                    [publication_status] => 1
                    [deletion_status] => 1
                )


        )

    [1] => Array
        (
            [0] => Array
                (
                    [category_id] => 28
                    [category_name] => Dry Food
                    [publication_status] => 1
                    [deletion_status] => 1
                )

            [1] => Array
                (
                    [category_id] => 39
                    [category_name] => Testt
                    [publication_status] => 1
                    [deletion_status] => 1
                )

            [2] => Array
                (
                    [category_id] => 37
                    [category_name] => Seasonal Items
                    [publication_status] => 1
                    [deletion_status] => 1
                )

            [3] => Array
                (
                    [category_id] => 38
                    [category_name] => Icon Items
                    [publication_status] => 1
                    [deletion_status] => 1
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [category_id] => 29
                    [category_name] => Biscuits
                    [publication_status] => 1
                    [deletion_status] => 1
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [category_id] => 30
                    [category_name] => Nutty
                    [publication_status] => 1
                    [deletion_status] => 1
                )

        )

)

Here is my loop:

foreach($result_array as $info) {
    echo $info['category_name'].'<br>';
}

This loop is giving me an error saying undefined index category_name. I am newbie in this field. Can anyone enlighten me up? Thanks

3
  • Nope @Siraj . My array is different than that. Answer is already given. I had to use foreach 2 times Commented Dec 30, 2017 at 15:41
  • Then read this answer stackoverflow.com/a/29363646/5132337 Commented Dec 30, 2017 at 15:42
  • 1
    Missed that. Got it Commented Dec 30, 2017 at 15:44

2 Answers 2

4

you need double foreach, like this:

foreach($result_array as $outer_array)
    foreach($outer_array as $inner_array)
        echo $inner_array['category_name'] . '<br>';
Sign up to request clarification or add additional context in comments.

Comments

1

The $result_array you are looping over has 4 elements and none of them have value with key category_name. You need to do double foreach or something wiser there.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.