Home > Backend Development > PHP Tutorial > Summarize the method of extracting a specified column from a multi-dimensional array in PHP

Summarize the method of extracting a specified column from a multi-dimensional array in PHP

coldplay.xixi
Release: 2023-04-09 13:34:01
forward
2904 people have browsed it

Summarize the method of extracting a specified column from a multi-dimensional array in PHP

The example of this article describes the method of extracting a specified column of a multi-dimensional array in PHP. Share it with everyone for your reference, the details are as follows:

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

Related learning recommendations: php programming (video)

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'),
);
Copy after login

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

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

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. Method :

$username = array_column($user, 'username');
Copy after login

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'];
});
Copy after login

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);
Copy after login

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;
}
Copy after login

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'];
}
Copy after login

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);
Copy after login

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);
Copy after login

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.

Related recommendations: Programming video course

The above is the detailed content of Summarize the method of extracting a specified column from a multi-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template