getColumn() public static method

The input array should be multidimensional or an array of objects. For example, php $array = [ ['id' => '123', 'data' => 'abc'], ['id' => '345', 'data' => 'def'], ]; $result = ArrayHelper::getColumn($array, 'id'); the result is: ['123', '345'] using anonymous function $result = ArrayHelper::getColumn($array, function ($element) { return $element['id']; });
public static getColumn ( array $array, string | Closure $name, boolean $keepKeys = true ) : array
$array array
$name string | Closure
$keepKeys boolean whether to maintain the array keys. If false, the resulting array will be re-indexed with integers.
return array the list of column values
示例#1
0
 /**
  * @param $users_info
  * @param $name
  * @return array
  */
 public function getUsersInfo($users_info, $name)
 {
     $array = [];
     //get needed column
     $users_info = array_filter(BaseArrayHelper::getColumn($users_info, $name, false));
     $users_info = array_count_values($users_info);
     foreach ($users_info as $key => $value) {
         $array[] = [$key, $value];
     }
     return $array;
 }