Combine all sub arrays into one

I am looking for a way to concatenate all child arrays into one big array.

array (
    [0] = 
         [0] = '0ARRAY',
         [1] = '1ARRAY'
    [1] = 
         [0] = '2ARRAY',
         [1] = '3ARRAY'
)

      

in

array (
    [0] = '0ARRAY', [1] = '1ARRAY', [2] = '2ARRAY', [3] = '3ARRAY'
)

      

No use array_merge($array[0],$array[1])

, because I don't know how many arrays there are. Therefore, I could not point them out.

thank

+3


source to share


3 answers


If it is only two levels of the array, you can use

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

      



which should work until $ array is completely empty

+9


source


If I understood your question:

php 5.6+

$array = array(
  array('first', 'second'),
  array('next', 'more')
);
$newArray = array_merge(...$array);

      

Outputs:



array(4) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(4) "next" [3]=> string(4) "more" }

      

Example: http://3v4l.org/KA5J1#v560

php <5.6

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

      

+5


source


$new_array = array();
foreach($main_array as $ma){
    if(!empty($ma)){
        foreach($ma as $a){
            array_push($new_array, $a);
        }
    }
}

      

You can try by posting these values:

$main_array[0][0] = '1';
$main_array[0][1] = '2';
$main_array[1][0] = '3';
$main_array[1][1] = '4';

      

OUTPUT:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) 

      

+2


source







All Articles