Summary of methods for converting PHP two-dimensional array (or arbitrary dimensional array) into one-dimensional array

In the process of PHP development, there are many array application scenarios. For example, PHP two-dimensional arrays (or arbitrary dimensional arrays) are converted into one-dimensional arrays. Today's article summarizes the method of converting PHP two-dimensional arrays (or arbitrary dimensional arrays) into one-dimensional arrays.

PHP二维数组(或任意维数组)转换成一维数组的方法总结

Suppose there is a two-dimensional array as follows:

 $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'), );

Now to convert to a one-dimensional array, there are two cases:

One is to convert the specified column into a one-dimensional array, which is summarized in another article: PHP extracts the value of a column of a two-dimensional array and converts it into a one-dimensional array.

Now let's focus on the second case, which is Convert all values into a one-dimensional array, and the same key value will not be overwritten , the converted one-dimensional array is as follows:

 $result = array(100, 'a1', 101, 'a2', 102, 'a3', 103, 'a4', 104, 'a5');

The implementation method is summarized as follows:

1. Array_reduce function method

use array_reduce() Function is a faster method:

 $result = array_reduce($user, function ($result, $value) { return array_merge($result, array_values($value)); }, array())

Note: The function () {} structure is a PHP anonymous function. For details, see: How to use PHP Closure function() use() {}

Because the array_merge function will overwrite and merge arrays with the same string key name, you must first use array_value to get the value before merging.

If the second dimension is a numeric key name, such as:

 $user = array( 'a' => array(100, 'a1'), 'b' => array(101, 'a2'), 'c' => array(102, 'a3'), 'd' => array(103, 'a4'), 'e' => array(104, 'a5'), );

Then it can be done directly:

 $result = array_reduce($user, 'array_merge', array())

 

2. Array_walk_recursive function method

use array_walk_recursive() The function is very flexible, and can convert an array of any dimension into a one-dimensional array.

 $result = []; array_walk_recursive($user, function($value) use (&$result) { array_push($result, $value); });

For example, the following multidimensional array:

 $user4 = array( 'a' => array( 100, 'a1'), 'b' => array(101, 'a2'), 'c' => array( 'd' => array(102, 'a3'), 'e' => array(103, 'a4'), ), );

With this method, it becomes:

 $result = array(100, 'a1', 101, 'a2', 102, 'a3', 103, 'a4');

 

3. Array_map function method

use array_map Similar to the array_reduce function, as follows:

 $result = []; array_map(function ($value) use (&$result) { $result = array_merge($result, array_values($value)); }, $user);

You just need to declare an empty $result array.

In addition, it can also be used array_walk The principle of this method is the same as that of the foreach loop.

Original link: http://www.cnblogs.com/isykw/p/6703493.html

 Watson Blog
  • This article is written by Published on November 19, 2017 00:15:04
  • 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/791.html

Comment