PHP » Arrays » array_merge()

Syntax:
array array_merge(array array1, array array2 [, array array3, ...])
array1
Array to merge with other arrays.
array2
Array to merge with other arrays.
array3
Array to merge with other arrays.

Merges a number of arrays into one.

This function returns one array with the elements of all the parameter arrays. If duplicate string indexes are encountered, later occurrences will overwrite earlier occurrences. Numerical indexes, however, are renumbered.

Examples

Code:
<pre>
<?php

$meal1 = array(
   "appetizer" => "shrimp",
   "main dish" => "steak",
   "dessert" => "icecream");
$meal2 = array(
   "drink" => "wine",
   "dessert" => "cake");

$array1 = array(1, 2, 3, 4, 5, 6);
$array2 = array(1, 2, 3, 7, 8, 9);

print_r(array_merge($meal1, $meal2));
print "<br>";
print_r(array_merge($array1, $array2));

?>
</pre>
Output:
Array
(
   [appetizer] => shrimp
   [main dish] => steak
   [dessert] => cake
   [drink] => wine
)
Array
(
   [0] => 1
   [1] => 2
   [2] => 3
   [3] => 4
   [4] => 5
   [5] => 6
   [6] => 1
   [7] => 2
   [8] => 3
   [9] => 7
   [10] => 8
   [11] => 9
)
Explanation:

This example shows what happens to the keys when array_merge() is used.

See Also: