从数组 php 中获取多列,array_column 的替代项

Get multi column from array php, Alternate of array_column

我已经使用这个函数从数组中获取多列。 array_column 的替代项。我创建这个函数是因为 array_column 不符合我的要求。

我的数据

$students = 
Array
(
    [0] => stdClass Object
        (
            [id] => 498
            [uuid] => 6cb91efd-9111-4be8-a2d7-80d3edeed732
            [name] => Andrew A. Blaine
            [email] => student14@gmail1.com
            [usertype_id] => 6
            [first_name] => Andrew A.
            [last_name] => Blaine
        )

    [1] => stdClass Object
        (
            [id] => 499
            [uuid] => 208764a0-c53d-404b-ad05-ee7cba28a51c
            [name] => Billie C. Heath
            [email] => student15@gmail1.com
            [usertype_id] => 6
            [first_name] => Billie C.
            [last_name] => Heath
        )
)

我的函数

  public function filterArrayByKeys($data, $keys = array()) {
    $filterData = array_map(function($e) use ($keys) {
        if (is_object($e)) {
            foreach ($keys as $key) {
                $filterArray[$key] = $e->$key;
            }
            return $filterArray;
        } else {
            foreach ($keys as $key) {
                $filterArray[$key] = $e[$key];
            }
            return $filterArray;
        }
    }, $data);
    return array_values(array_unique($filterData, SORT_REGULAR));
 }
 $students = $this->filterArrayByKeys($students, ['id', 'name', 'email']);

现在我的预期结果是:

$students = Array
(
    [0] => Array
        (
            [id] => 498
            [name] => Andrew A. Blaine
            [email] => student14@gmail1.com
        )

    [1] => Array
        (
            [id] => 499
            [name] => Billie C. Heath
            [email] => student15@gmail1.com
        )
)

您可以使用此函数从数组中获取多列。 希望对您有所帮助。

有自定义函数可以实现,

public function filterArrayByKeys(array $input, array $column_keys)
{
    $result      = array();
    $column_keys = array_flip($column_keys); // getting keys as values
    foreach ($input as $key => $val) {
         // getting only those key value pairs, which matches $column_keys
        $result[$key] = array_intersect_key($val, $column_keys);
    }
    return $result;
}

$a = $this->filterArrayByKeys($students, ['id','name','email']);
print_r($a);

array_flip — 交换数组中所有键及其关联值
array_intersect_key — 使用比较键计算数组的交集

输出

Array
(
    [0] => Array
        (
            [id] => 498
            [name] => Andrew A. Blaine
            [email] => student14@gmail1.com
        )

    [1] => Array
        (
            [id] => 499
            [name] => Billie C. Heath
            [email] => student15@gmail1.com
        )

)

工作demo

Source.

可以简单的使用array_map功能:

$fields = ['id', 'name', 'email'];

$newStudents = array_map(function ($student) use ($fields) {
    $newS = [];
    foreach ($fields as $field) {
        if (array_key_exists($field, $student)) {
            $newS[$field] = $student[$field];
        }
    }
    return $newS;
}, $students);

print_r($newStudents);

在此处查看演示 link:https://3v4l.org/AXiFL