Home  >  Article  >  Backend Development  >  What are the functions for converting php two-dimensional array to one-dimensional array?

What are the functions for converting php two-dimensional array to one-dimensional array?

L
LOriginal
2020-06-02 10:13:103192browse

What are the functions for converting php two-dimensional array to one-dimensional array?

php method of converting two-dimensional array to one-dimensional array

1. array_reduce function method

2. array_walk_recursive function method

3. array_map function method

Suppose there is the following two-dimensional 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'),
              );

Now we want to convert it into a one-dimensional array, there are two situations:

One is to convert the specified column into a one-dimensional array, which is summarized in another article: A complete list of methods for extracting a specified column from a multi-dimensional array in PHP.

Now we focus on the second case, which is to convert all values ​​into a one-dimensional array, and the key values ​​are the same and will not be overwritten. The converted one-dimensional array looks like this:

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

The main ones are as follows method.

1 array_reduceFunction method

Using the array_reduce() function is a faster method:

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

Because the array_merge function will merge the same string key name The arrays are overwritten and merged, so the values ​​must be taken out using array_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 just do this:

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

2 array_walk_recursiveFunction method

The array_walk_recursive() 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 multi-dimensional array:

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

After using this method, it becomes:

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

3 array_map function method

Use a The methods of the rray_map and array_reduce functions are similar, as follows:

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

Just need to declare an empty $result array.

In addition, you can also use the array_walk method and the foreach loop method. The principles are the same as above.

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of What are the functions for converting php two-dimensional array to one-dimensional array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn