10

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
)

7 Answers 7

20

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Jan I have a similar array which i've tried to merge using your example code. It's a nested array though so i've had a little trouble getting the arrays within the variable to merge. Here is how each array looks pastebin.com/rF8HUDS2 would you mind showing me how to implement your code with theses arrays.
@Anagio since your top-level answers are one-element arrays with only the result key, you need to map each element to its only element first. In 5.3+ you can write $input = array_map(function(x){return x['result']}, $input)
11

Since PHP 5.6 you can use variadics and argument unpacking.

$result = array_merge(...$input);

It's up to 3x times faster than call_user_func_array.

Comments

7

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 />"; 
}

1 Comment

how is that of any use?
3

array_merge can do the job

$array_meged = array_merge($a, $b);

after the comment

If fixed indexs you can use:

$array_meged = array_merge($a[0], $a[1]);

A more generic solution:

$array_meged=array();
  foreach($a as $child){
  $array_meged += $child;
}

Comments

2

$resultArray = array_merge ($array1, $array1);

$result = array();
foreach ($array1 as $subarray) {
    $result = array_merge($result, $subarray);
}

// Here it is done

Something good to read: https://www.php.net/manual/en/function.array-merge.php

Recursive:

https://www.php.net/manual/en/function.array-merge-recursive.php

5 Comments

there is only one array.. $array1 only and it has multiple arrays inside, how to merge them ?
that almost solves it but its still kinda complicated and annoying to do a loop and stuff, isn't there a single function to do that ?
ca2.php.net/manual/en/function.array-merge-recursive.php but it might not work exactly like you want it
I'm not sure what does it do really but their examples has 2 arrays as well, kinda same ? show an example
@Osa if you really hate loops, you can apply array_merge to the array. See my answer.
1
$arr1 = array(0=>1);
$arr2 = array(0=>2);

$merged = array_merge($arr1,$arr2);
print_r($merged);

Comments

0

array_merge is what you need.

$arr = array_merge($arr1, $arr2);

Edit:

$arr = array_merge($arr1[0], $arr1[1]);

2 Comments

there is only one array.. $arr1 only and it has multiple arrays inside, how to merge them ?
what if there are more than two elements in $arr1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.