Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge all sub arrays into one [duplicate]

Tags:

arrays

php

I'm looking to find a way to merge all child arrays into one large array.

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

into

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

Without using array_merge($array[0],$array[1]) because I don't know how many arrays there actually are. So I wouldn't be able to specify them.

Thanks

like image 263
Bri Avatar asked Dec 04 '14 21:12

Bri


1 Answers

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);
like image 187
Rangad Avatar answered Sep 18 '22 14:09

Rangad