PHP - Extracting a property from an array of objects

前端 未结 10 1013
时光取名叫无心
时光取名叫无心 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:18

    Warning create_function() has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.

    Builtin loops in PHP are faster then interpreted loops, so it actually makes sense to make this one a one-liner:

    $result = array();
    array_walk($cats, create_function('$value, $key, &$result', '$result[] = $value->id;'), $result)
    

提交回复
热议问题