Convert array of single-element arrays to one a dimensional array

I have this kind of an array:

Array
(
    [0] => Array
        (
            [0] => 88868
        )
    [1] => Array
        (
            [0] => 88867
        )
    [2] => Array
        (
            [0] => 88869
        )
    [3] => Array
        (
            [0] => 88870
        )
)

I need to convert this to one dimensional array. How can I do that?

For example like this..

Array
(
    [0] => 88868
    [1] => 88867
    [2] => 88869
    [3] => 88870 
)

Any php built in functionality is available for this array conversion?


Solution 1:

For your limited use case, this'll do it:

$oneDimensionalArray = array_map('current', $twoDimensionalArray);

This can be more generalized for when the subarrays have many entries to this:

$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);

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: Turning multidimensional array into one-dimensional array

Solution 3:

try:

$new_array = array();
foreach($big_array as $array)
{
    foreach($array as $val)
    {
        array_push($new_array, $val);
    }    
}

print_r($new_array);

Solution 4:

$oneDim = array();
foreach($twoDim as $i) {
  $oneDim[] = $i[0];
}