Several methods for PHP to extract the value of a column of a two-dimensional array into a one-dimensional array

 Watson Blog November 18, 2017 00:15:23 PHP technology comment three hundred and fifty-two Reading mode

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.

 Several methods for PHP to extract the value of a column of a two-dimensional array and convert it into a one-dimensional array

For example, for the following two-dimensional array:

  1. $wosn_net ?=? array (
  2. ????' 0'?=>? array ('id'?=>?100,?'username'?=>?'a1'),
  3. ????' 1'?=>? array ('id'?=>?101,?'username'?=>?'a2'),
  4. ????' 2'?=>? array ('id'?=>?102,?'username'?=>?'a3'),
  5. ????' 3'?=>? array ('id'?=>?103,?'username'?=>?'a4'),
  6. ????' 4'?=>? array ('id'?=>?104,?'username'?=>?'a5'),
  7. );

We need to extract the usename column and change it into the following one-dimensional array:

  1. $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:

  1. $username ?=? array_column( $wosn_net ,?' username');

Output results:

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

  1. $username ?=? array_column( $wosn_net ,?' username','id');

Output:

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

  1. $username ?=? array ();
  2. array_walk ( $wosn_net ,? function ( $value ,? $key )? use ? (& $username ){
  3. ???? $username []?=? $value ['username'];
  4. });

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.

  1. $username ?=? array ();
  2. array_map ( function ( $value )? use ? (& $username ){
  3. ???? $username []?=? $value ['username'];
  4. },? $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:

  1. $username ?=? array_reduce ( $wosn_net ,? create_function(' $result ,? $v ',?' $result []?=? $v [ "username" ]; return ? $result ;'));

Output:

  1. 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.

  1. $username ?=? array ();
  2. foreach ? ( $wosn_net ? as ? $value )? {
  3. ???? $username []?=? $value ['username'];
  4. }

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.

  1. $username ?=? array_map (' array_shift ',? $wosn_net );

The output is as follows:

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

  1. $username ?=? array_map ('reset',? $wosn_net );
  2. $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

 Watson Blog
  • This article is written by Published on November 18, 2017 00:15:23
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/790.html

Comment