Append to an existing array?


Staff member
Does anybody know how to add values from an array into an array that has already been defined?

Basically, Array 1 has been opened and closed, now a few sections down I need to enter more variables into the array, but through another array. Obviously I can do this by adding them directly into the firste array, but the second array is being pulled from a separate php file.

I hope that makes sense. If anybody has any ideas I would appreicate it.


Registered Member
The following is how you merge one array into another.

$array1 = array(2, 4);
$array2 = array("a", "b", 4);
$result = array_merge($array1, $array2);
The above example will output:

[0] => 2
[1] => 4
[2] => a
[3] => b
[4] => 4

array_merge($array1, $array2) takes the values in $array2 and adds them to the back of $array1 and then returns the result as a new array.

Hope this is helpful.