duandu2159 2016-02-20 13:51
浏览 13
已采纳

PHP如何破坏这个多个数组?

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.

  • 写回答

1条回答 默认 最新

  • dszn2485 2016-02-20 13:57
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?