Turning multidimensional array into one-dimensional array [duplicate]

Solution 1:

array_reduce($array, 'array_merge', array())

Example:

$a = array(array(1, 2, 3), array(4, 5, 6));
$result = array_reduce($a, 'array_merge', array());

Result:

array[1, 2, 3, 4, 5, 6];

Solution 2:

The PHP array_merge­Docs function can flatten your array:

$flat = call_user_func_array('array_merge', $array);

In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:

$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);

See as well: How to Flatten a Multidimensional Array?.

Solution 3:

As of PHP 5.6 this can be done more simply with argument unpacking.

$flat = array_merge(...$array);

Solution 4:

This is really all there is to it:

foreach($array as $subArray){
    foreach($subArray as $val){
        $newArray[] = $val;
    }
}