Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple arrays from one array

How to merge multiple arrays from a single array variable ? lets say i have this in one array variable

Those are in one variable ..
$array = array(array(1), array(2));

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

how to end up with this

Array
(
   [0] => 1
   [1] => 2
)
like image 540
Osa Avatar asked Nov 24 '12 19:11

Osa


2 Answers

This is the PHP equivalent of javascript Function#apply (generate an argument list from an array):

$result = call_user_func_array("array_merge", $input);

demo: http://3v4l.org/nKfjp

like image 117
John Dvorak Avatar answered Sep 18 '22 16:09

John Dvorak


This may work:

$array1 = array("item1" => "orange", "item2" => "apple", "item3" => "grape");
$array2 = array("key1" => "peach", "key2" => "apple", "key3" => "plumb");
$array3 = array("val1" => "lemon");

$newArray = array_merge($array1, $array2, $array3);

foreach ($newArray as $key => $value) {
  echo "$key - <strong>$value</strong> <br />"; 
}
like image 44
1990rk4 Avatar answered Sep 20 '22 16:09

1990rk4