Flatten an array in PHP
This article demonstrates how to flatten an array in PHP.
1. Using iterator_to_array() function
You can flatten an array in PHP using a RecursiveArrayIterator within a RecursiveIteratorIterator. When used with the SPL function iterator_to_array(), RecursiveIteratorIterator returns a flattened array. A typical implementation might look something like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php $arr = array(1, 2, array(3, array(4, array(5, 6), 7)), array(8, 9)); $it = new RecursiveArrayIterator($arr); $iterator = new RecursiveIteratorIterator($it); $flattened_arr = iterator_to_array($iterator, false); print_r($flattened_arr); /* Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) */ ?> |
The second parameter to the iterator_to_array() function indicates whether to use the iterator element keys as indexes. If this parameter is true (or unset), elements with the same keys will be overwritten, and the returned array will contain the last value associated with the duplicate key. However, if this parameter is false, it returns all the values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php $arr = array(array('Germany' => 'Berlin', 'UK' => 'London'), array('Netherlands' => 'Amsterdam', array('Netherlands' => 'AMSTERDAM', 'France' => 'Paris') )); $it = new RecursiveArrayIterator($arr); $iterator = new RecursiveIteratorIterator($it); $flattened_arr = iterator_to_array($iterator); print_r($flattened_arr); /* Array ( [Germany] => Berlin [UK] => London [Netherlands] => AMSTERDAM [France] => Paris ) */ $values = iterator_to_array($iterator, false); print_r($values); /* Array ( [0] => Berlin [1] => London [2] => Amsterdam [3] => AMSTERDAM [4] => Paris ) */ ?> |
2. Using array_walk_recursive() function
Alternatively, you can use array_walk_recursive() function to flatten arrays. It recursively applies the user-defined function to each element of the array.
Following is a simple solution that walks a nested array using array_walk_recursive, and inserts each encountered element into the output array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php $arr = array(1, 2, array(3, array(4, array(5, 6), 7)), array(8, 9)); $flattened_arr = array(); array_walk_recursive($arr, function($value) use (&$flattened_arr) { $flattened_arr[] = $value; }); print_r($flattened_arr); /* Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) */ ?> |
Here’s equivalent code for an associative array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php $arr = array(array('Germany' => 'Berlin', 'UK' => 'London'), array('Netherlands' => 'Amsterdam', array('Netherlands' => 'AMSTERDAM', 'France' => 'Paris') )); $flattened_arr = array(); array_walk_recursive($arr, function($value, $key) use (&$flattened_arr) { $flattened_arr[$key] = $value; }); print_r($flattened_arr); /* Output: Array ( [Germany] => Berlin [UK] => London [Netherlands] => AMSTERDAM [France] => Paris ) */ ?> |
3. Using array_reduce() function
The array_reduce() function iteratively applies a callback function to the elements of the array, so as to reduce the array to a single value.
You can reduce a two-dimensional array into a one-dimensional array with array_reduce() using array_merge() as the callback. The array_merge() function merges elements of one or more arrays and returns the resulting array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php $arr = array( array('Germany' => 'Berlin', 'UK' => 'London'), array('Netherlands' => 'Amsterdam'), array('Netherlands' => 'AMSTERDAM', 'France' => 'Paris') ); $flattened_arr = array_reduce($arr, 'array_merge', array()); print_r($flattened_arr); /* Output: Array ( [Germany] => Berlin [UK] => London [Netherlands] => AMSTERDAM [France] => Paris ) */ ?> |
Note that this only works for two-dimensional arrays. For numeric 2D arrays, you can unpack the array with the ... operator and directly invoke the array_merge() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $arr = array(array(1, 2), array(3, 4, 5)); $flattened_arr = array_merge(...$arr); print_r($flattened_arr); /* Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) */ ?> |
That’s all about flattening an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)