-1

I have the following object:

stdClass Object

(
    [ID] => 6
    [data] => stdClass Object
        (

        [categories] => Array
            (
                [23] => Array
                    (
                        [id] => 23
                        [name] => A
                    )

                [22] => Array
                    (
                        [id] => 22
                        [name] => B
                    )

                [19] => Array
                    (
                        [id] => 19
                        [name] => C
                    )

            )
)

I would like to print A,B,C. I managed to print 1 name: echo $event->data->categories[19]['name']; but I would like to print all names of the array without knowing the id's.

Dave
  • 27
  • 6

2 Answers2

1

You can use foreach to loop through the categories array like this:

foreach($event->data->categories as $category) {
    echo $category['name'];
}
catcon
  • 1,295
  • 1
  • 9
  • 18
1

You can use array_column and implode

echo implode(', ', array_column($event->data->categories, 'name'));

This will get all name items and implode them to a string.

Andreas
  • 23,610
  • 6
  • 30
  • 62