Mirakurun Mirakurun - 4 years ago 133
PHP Question

Append elements of an array to another array

I have some unknown number of iterations in which every iteration gives two arrays.

for ($i = 0; $i<sizeof($foo); $i++) {
$array1 = //do something
$array2 = //do something
$result = //($result + $array1 + $array2)
}


What I want to do is to append the elements of those arrays to
$result
.

If I use
array_merge()
I cannot add the previous elements of
$result
to it.

If I use
array_push()
I will get a 2D array which I don't want.

array_push($result, $array1, $array2);


So what is the best solution to my problem? Is there any way to do it without iterating through each array and pushing every element?

Answer Source

The solution using call_user_func_array function:

...
$result = call_user_func_array("array_merge", [$result, $array1, $array2]);
...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download