Get specific element from each sub array

后端 未结 2 843
旧时难觅i
旧时难觅i 2020-12-25 11:04

I have a common pattern which Im sure there must be a built-in array function in PHP to handle but just can\'t see it.

I have multiple arrays such as the following:<

2条回答
  •  眼角桃花
    2020-12-25 11:50

    Before PHP 5.5, this would be the most efficient solution:

    $key = 'key1';
    
    $output = array_map(function($item) use ($key) {
        return $item[$key];
    }, $testArray);
    

    As of PHP 5.5, there is now an array_column function for this (see COil's answer).

提交回复
热议问题