0

Sorry guys, I am new in programmer and I am stuck in this code, I want to implode this array and get result like this: Action, Adventure, Comedy, Science Fiction

$input = Array (

[0] => stdClass Object
    (
        [id] => 28
        [name] => Action
    )

[1] => stdClass Object
    (
        [id] => 12
        [name] => Adventure
    )

[2] => stdClass Object
    (
        [id] => 35
        [name] => Comedy
    )

[3] => stdClass Object
    (
        [id] => 878
        [name] => Science Fiction
    )

)

I am trying do like this and always getting error messages:

echo implode(', ', array_map(function ($entry) {
  return $entry['name'];
}, $input));

or

echo implode(', ', array_column($input, 'name'));

Thanks for help.

Ajie Kurniyawan
  • 393
  • 2
  • 18

1 Answers1

3

The below should work. I have changed the return of the anonymous function to return the objects name property instead of an array entry.

echo implode(', ', array_map(function ($entry) {
    return $entry->name;
}, $input));
awinwood
  • 135
  • 7