dongtaigan1594 2014-02-07 22:34
浏览 14
已采纳

PHP中的Ruby映射函数?

I am really used to programming in Ruby. I am working in PHP now.

I have an array of objects, and I want to get all the ids of those objects and put them in an array.

In Ruby you would do:

my_arr.map { |arr| arr.id }

How would I do that in PHP? Do I need to do a foreach and push that id into an array?

  • 写回答

1条回答 默认 最新

  • duanpi2033 2014-02-07 22:38
    关注

    You have array_map. For example:

    $my_arr = array_map(function($el) { 
        return $el['id'];
    }, $arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?