


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!

php求2个数组相同元素的方法:1、创建一个php示例文件;2、定义两个有相同元素的数组;3、使用“array_intersect($array1,$array2)”或“array_intersect_assoc()”方法获取两个数组相同元素即可。

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

增加元素的方法:1、使用unshift()函数在数组开头插入元素;2、使用push()函数在数组末尾插入元素;3、使用concat()函数在数组末尾插入元素;4、使用splice()函数根据数组下标,在任意位置添加元素。

php判断数组里面是否存在某元素的方法:1、通过“in_array”函数在数组中搜索给定的值;2、使用“array_key_exists()”函数判断某个数组中是否存在指定的key;3、使用“array_search()”在数组中查找一个键值。

php去除第一个数组元素的方法:1、新建一个php文件,并创建一个数组;2、使用“array_shift”方法删除数组首个元素;3、通过“print_”r输出数组即可。

在Go语言中,数组是一种重要的数据类型。它与其他语言的数组一样,是一组相同类型的数据组成,可以通过一个索引来访问数组中的元素。在某些情况下,我们需要从一个数组中删除元素,本文将会介绍在Go语言中如何实现数组删除。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use