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