PHP - Extracting a property from an array of objects

前端 未结 10 999
时光取名叫无心
时光取名叫无心 2020-12-02 05:16

I\'ve got an array of cats objects:

$cats = Array
    (
        [0] => stdClass Object
            (
                [id] => 15
            ),
                 


        
10条回答
  •  醉酒成梦
    2020-12-02 05:21

    You can do it easily with ouzo goodies

    $result = array_map(Functions::extract()->id, $arr);
    

    or with Arrays (from ouzo goodies)

    $result = Arrays::map($arr, Functions::extract()->id);
    

    Check out: http://ouzo.readthedocs.org/en/latest/utils/functions.html#extract

    See also functional programming with ouzo (I cannot post a link).

提交回复
热议问题