The extraction of a column from a two-dimensional array in PHP is a very common and frequently used function. Because of this, After PHP 5.5.0, a special function array_column() has been added. Of course, if your PHP version is lower than 5.5.0, you have to use other methods.
For example, for the following two-dimensional array:
- $wosn_net ?=? 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 need to extract the usename column and change it into the following one-dimensional array:
- $username ?=? array ('a1',?'a2',?'a3',?'a4',?'a5');
The treatment methods are as follows:
1、 Array_column function method
array_column It is the simplest method, but the PHP version must be 5.5.0 and above. Method:
- $username ?=? array_column( $wosn_net ,?' username');
Output results:
- Array? (?[0]?=>? a1? [1]?=>? a2? [2]?=>? a3? [3]?=>? a4? [4]?=>? a5?)
Note: array_column(); The third parameter can be used to return the index/key column of the array. It can be the integer index of the column or the string key value. For example:
- $username ?=? array_column( $wosn_net ,?' username','id');
Output:
- Array? (?[100]?=>? a1? [101]?=>? a2? [102]?=>? a3? [103]?=>? a4? [104]?=>? a5?)
Note the ID difference between the two outputs above;
2、 Array_walk function method
array_walk() The function uses user-defined functions to callback each element in the array, and the method to realize the current function is as follows:
- $username ?=? array ();
- array_walk ( $wosn_net ,? function ( $value ,? $key )? use ? (& $username ){
- ???? $username []?=? $value ['username'];
- });
Note: "function() use() {}" is a way to write php closures. For details, see: How to use PHP Closure function() use() {} 。
3、 Array_map function method
array_map() The array_walk() function is similar to array_walk() in that it applies the callback function to the cell of a given array.
- $username ?=? array ();
- array_map ( function ( $value )? use ? (& $username ){
- ???? $username []?=? $value ['username'];
- },? $wosn_net );
4、 Array_reduce function method
use array_reduce The method has a little more code, but the imagination space of this method (for other array value operations) is quite large:
- $username ?=? array_reduce ( $wosn_net ,? create_function(' $result ,? $v ',?' $result []?=? $v [ "username" ]; return ? $result ;'));
Output:
- Array? (?[0]?=>? a1? [1]?=>? a2? [2]?=>? a3? [3]?=>? a4? [4]?=>? a5?)
The array_reduce method uses a callback function to iteratively operate on the value of the array, while create_function is used to call back an anonymous method. The parameter $result of this anonymous method is the value generated in the previous iteration, and $v is the current value. The internal implementation is to obtain the value of "username" of each item of the array $arr and push it to the new $result array;
5、 Foreach cycle method
foreach The loop is a little less efficient than the above method, but it is simple and easy to understand.
- $username ?=? array ();
- foreach ? ( $wosn_net ? as ? $value )? {
- ???? $username []?=? $value ['username'];
- }
Note: My writing method is to directly foreach, and then array_push one by one into an array variable. This writing method may have a slight impact on performance, because using PHP native functions is certainly more efficient than looping.
6、 Array_map variant
The method is as follows, which means to remove the beginning value of each item value of the $wosn_net array, and obtain the removed value as a new array. Note that the key of the new array $username is still the key of the original array $wosn_net, as shown below.
- $username ?=? array_map (' array_shift ',? $wosn_net );
The output is as follows:
- Array? (?[0]?=>?100?[1]?=>?101?[2]?=>?102?[3]?=>?103?[4]?=>?104?)
Note: This function will obtain the id column in $wosn_net instead of the username column. The key of the output array is the key of the $wosn_net array, which coincides with no visible effect.
In addition, if you need to obtain the beginning or end columns of each item in the two-dimensional array, you can also do this:
- $username ?=? array_map ('reset',? $wosn_net );
- $username ?=? array_map (' end ',? $wosn_net );
These three variants have limited functions, and are only useful for obtaining the first or last column. They are difficult to work in complex arrays.
Reference link
https://www.awaimai.com/885.html
http://blog.it985.com/15036.html
http://blog.csdn.net/liruxing1715/article/details/22925575