Combine the values of two or more arrays into a new array. Values will be appended onto the end with numeric keys being re-indexed as it goes. Should a duplicate non-numeric key be found, the previous value is overwritten.

You might think of this as adding arrays together, which is fine, but you can actually use + with arrays. More about that after a few examples.

$a1 = array('a',
            'b',
            'c',
            'd',
            'e');
 
$a2 = array('p',
            'o',
            'i',
            'u');
 
$arr = array_merge($a1, $a2);
/*
array (
  0 => 'a',
  1 => 'b',
  2 => 'c',
  3 => 'd',
  4 => 'e',
  5 => 'p',
  6 => 'o',
  7 => 'i',
  8 => 'u'
)
*/
 
$a1 = array('a',
            'b',
            'this' => 'c',
            'd',
            'e');
 
$a2 = array('p',
            'that' => 'o',
            'i',
            'u');
 
$arr = array_merge($a1, $a2);
/*
array (
  0 => 'a',
  1 => 'b',
  'this' => 'c',
  2 => 'd',
  3 => 'e',
  4 => 'p',
  'that' => 'o',
  5 => 'i',
  6 => 'u'
)
*/
 
// even the first array is re-indexed
// 'this' value is overwritten
$a1 = array(5 => 'a',
            'b',
            'this' => 'c',
            'd',
            'e');
 
$a2 = array(17 => 'p',
            'this' => 'o',
            'i',
            'u');
 
$arr = array_merge($a1, $a2);
/*
array (
  0 => 'a',
  1 => 'b',
  'this' => 'o',
  2 => 'd',
  3 => 'e',
  4 => 'p',
  5 => 'i',
  6 => 'u'
)
*/
 
// if only one array is given, it is just re-indexed
$arr = array_merge($a1);
/*
array (
  0 => 'a',
  1 => 'b',
  'this' => 'c',
  2 => 'd',
  3 => 'e'
)
*/

Remember, this function will accept more than two arrays.

$a1 = range(3, 7);
$a2 = range('A', 'D');
$a3 = array(2001 => 'oddity');
$a4 = range('i', 'k');
 
$arr = array_merge($a1, $a2, $a3, $a4);
/*
array (
  0 => 3,
  1 => 4,
  2 => 5,
  3 => 6,
  4 => 7,
  5 => 'A',
  6 => 'B',
  7 => 'C',
  8 => 'D',
  9 => 'oddity',
  10 => 'i',
  11 => 'j',
  12 => 'k'
)
*/

Using + to merge arrays together acts differently. The keys are not re-indexed, and values are not overwritten. For duplicate keys (even numeric ones) the previous value stands and the other is ignored.

$a1 = array('a', 'b');
$a2 = array('A', 'B', 'C', 'D');
$a3 = array('m' => 13,
            'n' => 14);
$a4 = array(17 => 'Q',
            18 => 'R');
 
$arr = $a1 + $a2 + $a3 + $a4;
/*
array (
  0 => 'a',
  1 => 'b',
  2 => 'C',
  3 => 'D',
  'm' => 13,
  'n' => 14,
  17 => 'Q',
  18 => 'R'
)
*/