Home  >  Article  >  Backend Development  >  A complete list of methods for extracting a specified column from a multidimensional array in PHP

A complete list of methods for extracting a specified column from a multidimensional array in PHP

angryTom
angryTomforward
2019-10-16 09:37:162717browse

A complete list of methods for extracting a specified column from a multidimensional array in PHP

The extraction of specific columns of multi-dimensional arrays is a very common function in PHP. Because of this, PHP added a dedicated function array_column after version 5.5.0 (). Of course, if your PHP version is lower than 5.5.0, you have to use other methods.

For example, for the following array:

$user = array(
    '0' => array('id' => 100, 'username' => 'a1'),
    '1' => array('id' => 101, 'username' => 'a2'),
    '2' => array('id' => 102, 'username' => 'a3'),
    '3' => array('id' => 103, 'username' => 'a4'),
    '4' => array('id' => 104, 'username' => 'a5'),
);

We want to extract the usename column and turn it into:

$username = array('a1', 'a2', 'a3', 'a4', 'a5');

There are several methods.

1 array_column function method

Using PHP's built-in array_column() function is the simplest method. The limitation is that the PHP version must be 5.5.0 and above. The method :

$username = array_column($user, 'username');

2 array_walk function method

array_walk() function uses a user-defined function to perform callback processing on each element in the array to implement the current function:

$username = array();
array_walk($user, function($value, $key) use (&$username){
    $username[] = $value['username'];
});

3 array_map function method

The array_map() function is similar to array_walk(), applying the callback function to the cells of the given array.

$username = array();
array_map(function($value) use (&$username){
    $username[] = $value['username'];
}, $user);

In actual use, we can use array_map() to write a function with the same function as PHP's built-in array_column():

/**
 * 获取二维数组指定的一列,并以一维数组格式返回
 * 作用和PHP5.5.0中的array_column()函数一样
 * @param $input array 需要取出数组列的多维数组(或结果集)
 * @param $column_key string 需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键。 也可以是NULL,此时将返回整个数组
 * @param $index_key string 作为返回数组的索引/键的列,它可以是该列的整数索引,或者字符串键值。
 * @return array|null
 */
function array_column($input, $column_key, $index_key = null) {
    $arr = array_map(function($d) use ($column_key, $index_key) {
        if (!isset($d[$column_key])) {
            return null;
        }
        if ($index_key !== null) {
            return array($d[$index_key] => $d[$column_key]);
        }
        return $d[$column_key];
    }, $input);
    if ($index_key !== null) {
        $tmp = array();
        foreach ($arr as $ar) {
            $tmp[key($ar)] = current($ar);
        }
        $arr = $tmp;
    }
    return $arr;
}

4 foreach loop method

The foreach loop is slightly less efficient than the above method, but it is simple and easy to understand.

$username = array();
foreach ($user as $value) {
    $username[] = $value['username'];
}

5 array_map variant

The method is as follows, which means to remove the starting value of each value of the $user array and obtain the removed value as a new array . Note that the keys of the new array $username are still the keys of the original array $user, as follows.

$username = array_map('array_shift', $user);

Note: This function will get the id column in $user, not the username column.

In addition, if you need to get the beginning column or the end column of each item of the two-dimensional array, you can also do this:

$username = array_map('reset', $user);
$username = array_map('end', $user);

The effect of these three variant methods is relatively limited. They only obtain the first column. It is useful for the first column or the last column, but it is difficult to play a role in complex arrays.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of A complete list of methods for extracting a specified column from a multidimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.awaimai.com. If there is any infringement, please contact admin@php.cn delete