array_mergeMERGES the elements of one or more ARRAYS together so that the values of one are appended to the end of the previous one.
If $array1, $array2, ..., $arrayN have the same string keys, then the later value for that key will overwrite the previous one.
If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in $array1, $array2, ..., $arrayN with numeric keys will be renumbered with incrementing keys starting from zero in the result ARRAY.
If this function called without any arguments, returns an empty ARRAY.
In PHP 7.4.0, this function can now be called without any parameter but, formerly, at least one parameter has been required.
<?php
arr array_merge ( [ arr $array2, ..., $arrayN ] )
where,
$array1 = The first ARRAY of the list of ARRAYS to merge
$array2 = The second ARRAY of the list of ARRAYS to merge
. . . . . . . . . . .
$arrayN = The last ARRAY of the list of ARRAYS to merge
?>
$array1
The first ARRAY of the list of ARRAYS to merge.
$array2
The second ARRAY of the list of ARRAYS to merge.
$arrayN
The last ARRAY of the list of ARRAYS to merge.
EXERCISE
<?php
$nbr01a = [ 1, 2, 3, 4, 5, 6, 7 ];
$nbr01b = [ 8, 9, 10 ];
$nbr01c = [ 11, 12, 13, 14, 15 ];
echo 'FIRST ARRAY<br><pre>';
print_r($nbr01a);
echo '</pre><br><br>';
echo 'SECOND ARRAY<br><pre>';
print_r($nbr01b);
echo '</pre><br><br>';
echo 'THIRD ARRAY<br><pre>';
print_r($nbr01c);
echo '</pre><br><br>';
echo 'ARRAY after array_merge<br><br>';
$nbr01abc = array_merge ($nbr01a, $nbr01b, $nbr01c);
echo '<pre>';
print_r($nbr01abc);
echo '</pre><br><br>';
?>
RESULT
FIRST ARRAY
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
SECOND ARRAY
Array
(
[0] => 8
[1] => 9
[2] => 10
)
THIRD ARRAY
Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
[4] => 15
)
ARRAY - after array_merge
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => 12
[12] => 13
[13] => 14
[14] => 15
EXERCISE
<?php
$nbr02a = [ 'one', 'two', 'three', 'four' ];
$nbr02b = [ 'five', 'six', 'seven' ];
$nbr02c = [ ];
echo 'FIRST ARRAY<br><pre>';
print_r($nbr02a);
echo '</pre><br><br>';
echo 'SECOND ARRAY<br><pre>';
print_r($nbr02b);
echo '</pre><br><br>';
echo 'THIRD ARRAY<br><pre>';
print_r($nbr02c);
echo '</pre><br><br>';
echo 'ARRAY after array_merge<br><br>';
$nbr02abc = array_merge ($nbr02a, $nbr02b, $nbr02c);
echo '<pre>';
print_r($nbr02abc);
echo '</pre><br><br>';
?>
RESULT
FIRST ARRAY
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
SECOND ARRAY
Array
(
[0] => five
[1] => six
[2] => seven
)
THIRD ARRAY
Array
(
)
ARRAY - after array_merge
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
[4] => five
[5] => six
[6] => seven
)
EXERCISE
<?php
$nbr03a = [ ];
$nbr03b = [ ];
$nbr03c = [ ];
echo 'FIRST ARRAY<br><pre>';
print_r($nbr03a);
echo '</pre><br><br>';
echo 'SECOND ARRAY<br><pre>';
print_r($nbr03b);
echo '</pre><br><br>';
echo 'THIRD ARRAY<br><pre>';
print_r($nbr03c);
echo '</pre><br><br>';
echo 'ARRAY after array_merge<br><br>';
$nbr03abc = array_merge ($nbr03a, $nbr03b, $nbr03c);
echo '<pre>';
print_r($nbr03abc);
echo '</pre><br><br>';
?>
RESULT
FIRST ARRAY
Array
(
)
SECOND ARRAY
Array
(
)
THIRD ARRAY
Array
(
)
ARRAY - after array_merge
Array
(
)
EXERCISE
<?php
$nbr04a = [ ];
echo 'UNIQUE ARRAY<br><pre>';
print_r($nbr04a);
echo '</pre><br><br>';
echo 'ARRAY after array_merge<br><br>';
$nbr04abc = array_merge ($nbr04a);
echo '<pre>';
print_r($nbr04abc);
echo '</pre>';
?>
RESULT
UNIQUE ARRAY
Array
(
)
ARRAY - after array_merge
Array
(
)
EXERCISE
<?php
$arrm05a = array("R" => "RED",
"G" => "GREEN",
"B" => "BLUE");
$arrm05b = array("H" => "HUE",
"S" => "SATURATION",
"K" => "BLACK");
echo 'INITIAL ARRAYS:<br><pre>';
print_r($arrm05a);
echo '<br><br>';
print_r($arrm05b);
echo '</pre>';
$arrm05ab = array_merge($arrm05a, $arrm05b);
echo '<br><br><br>ARRAY atter array_merge:<br><pre>';
print_r($arrm05ab);
echo '</pre>';
?>
EXERCISE
<?php
$date06 = date(DateTimeInterface::RFC850);
$arrm06a = [ $date06, 'countries' => [ 'Brasil', 'Portugal' ] ];
$arrm06b = [ 'continents' => [ 'South America', 'Europe' ] ];
echo '<br><br>INITIAL ARRAYS:<br><pre>';
print_r($arrm06a);
echo '<br><br>';
print_r($arrm06b);
echo '</pre><br><br>';
$arrm06ab = array_merge($arrm06a, $arrm06b);
echo '<br><br><br>RESULTING ARRAY:<br><pre>';
print_r($arrm06ab);
echo '</pre><br><br>';
?>
EXERCISE
<?php
$arrm07a = [ 'countries' => [ 'Brasil', 'Portugal' ] ];
$arrm07b = [ 'countries' => [ 'Argentina', 'Espanha' ] ];
echo 'INITIAL ARRAYS:<br><pre>';
print_r($arrm07a);
echo '</pre><br><br><pre>';
print_r($arrm07b);
$arrm07ab = array_merge($arrm07a, $arrm07b);
echo '<br><br><br>RESULTING ARRAY:<br><pre>';
print_r($arrm07ab);
echo '</pre>';
?>
EXERCISE
<?php
setlocale(LC_ALL, 'de-DE', 'de_DE');
// Only in PHP versions below 8.0.XX.
echo "Testing basic functionality:<br><br>";
$begin_array = array(
array(),
array( 1 => "string"),
array( "" => "string"),
array( -2.44444 => 12),
array( "a" => 1, "b" => -2.344, "b" => "string",
"c" => NULL, "d" => -2.344),
array( 4 => 1, 3 => -2.344,
"3" => "string", "2" => NULL,1 => -2.344),
array( NULL, 1.23 => "Hi", "string" => "hello",
array("" => "World", "-2.34" => "a", "0" => "b"))
);
$end_array = array(
array(),
array( 1 => "string"),
array( "" => "string"),
array( -2.44444 => 12),
array( "a" => 1, "b" => -2.344, "b" => "string",
"c" => NULL, "d" => -2.344),
array( 4 => 1, 3 => -2.344, "3" => "string",
"2" => NULL, 1=> -2.344),
array( NULL, 1.23 => "Hi", "string" => "hello",
array("" => "World", "-2.34" => "a", "0" => "b"))
);
echo 'First ARRAY:<br>';
var_dump($begin_array);
echo '<br><br>End ARRAY:<br>';
var_dump($end_array);
/* loop through to merge two arrays */
$count_outer = 0;
foreach($begin_array as $first) {
echo "<br><br>Iteration: $count_outer<br>";
$count_inner = 0;
foreach($end_array as $second) {
echo "<br>Inner iteration: $count_inner
of Iteration: $count_outer<br>";
$result = array_merge($first, $second);
print_r($result);
$count_inner++;
}
$count_outer++;
}
echo "<br><br><br>Testing with three or more arrays:<br>";
var_dump( array_merge( $end_array[0],
$end_array[5],
$end_array[4],
$end_array[6]
)
);
echo '<br><br>';
var_dump( array_merge( $end_array[0],
$end_array[5],
array("array on fly"),
array("nullarray" => array())
)
);
echo "<br><br><br>Testing single array argument:<br><br>";
/* Empty array */
var_dump(array_merge(array()));
echo '<br><br>';
/* associative array with string keys,
which will not be re-indexed */
var_dump(array_merge($begin_array[4]));
echo '<br><br>';
/* associative array with numeric keys,
which will be re-indexed */
var_dump(array_merge($begin_array[5]));
echo '<br><br>';
/* associative array with mixed keys
and sub-array as element */
var_dump(array_merge($begin_array[6]));
echo "<br><br><br>Testing array_merge() with typecasting non-array to array:<br>";
var_dump(array_merge($begin_array[4], (array)"type1",
(array)10, (array)12.34));
echo "<br><br><br>Testing array_merge without any arguments:<br>";
var_dump(array_merge());
?>
EXERCISE
<?php
/*
* Test basic functionality of array_merge()
*/
echo "Testing basic functionality:<br><br>";
//indexed array
$array1 = array ('zero', 'one', 'two');
//associative array
$array2 = array ('a' => 1, 'b' => 2, 'c' => 3);
echo 'The given ARRAY(1)<br>';
print_r($array1);
echo '<br><br>The given ARRAY(2)<br>';
print_r($array2);
echo '<br><br>Merging 1 to 2:<br>';
var_dump(array_merge($array1, $array2));
echo '<br><br>Merging 2 to 1:<br>';
var_dump(array_merge($array2, $array1));
?>
EXERCISE
<?php
/*
* Check the position of the
* internal array pointer
* after calling array_merge().
* This test is also passing more than
* two arguments to array_merge().
*/
echo "Testing usage variations:<br><br>";
$arr1 = array ('zero', 'one', 'two');
$arr2 = array ('zero', 'un', 'deux');
$arr3 = array ('null', 'eins', 'zwei');
echo 'The given ARRAY(1)<br>';
print_r($arr1);
echo '<br><br>The given ARRAY(2)<br>';
print_r($arr2);
echo '<br><br>The given ARRAY(3)<br>';
print_r($arr3);
echo "<br><br>Call array_merge():<br>";
var_dump($result = array_merge($arr1, $arr2, $arr3));
echo "<br><br>Position of Internal Pointer in Result:<br>";
echo key($result) . " => " . current($result) . "<br>";
echo "<br>Position of Internal Pointer in Original Array:<br><br>";
echo "\$arr1: ";
echo key($arr1) . " => " . current ($arr1) . "<br>";
echo "\$arr2: ";
echo key($arr2) . " => " . current ($arr2) . "<br>";
echo "\$arr3: ";
echo key($arr3) . " => " . current ($arr3) . "<br>";
?>
EXERCISE
<?php
/*
* Pass an array with different
* data types as keys to test how array_merge
* adds it onto an existing array
*/
echo "Testing usage variations:<br><br>";
// Initialise function arguments
// not being substituted
$arr = array ('one' => 1, 'two' => 2);
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
hello world
EOT;
// arrays with keys as different
// data types to be passed as $input
$inputs = array(
// int data
/*1*/ 'int' => array(
0 => 'zero',
1 => 'one',
12345 => 'positive',
-2345 => 'negative',
),
// float data
/*2*/ 'float' => array(
10.5 => 'positive',
-10.5 => 'negative',
.5 => 'half',
),
/*3*/ 'extreme floats' => array(
12.3456789000e10 => 'large',
12.3456789000E-10 => 'small',
),
// null data
/*4*/ 'null uppercase' => array(
NULL => 'null 1',
),
/*5*/ 'null lowercase' => array(
null => 'null 2',
),
// boolean data
/*6*/ 'bool lowercase' => array(
true => 'lowert',
false => 'lowerf',
),
/*7*/ 'bool uppercase' => array(
TRUE => 'uppert',
FALSE => 'upperf',
),
// empty data
/*8*/ 'empty double quotes' => array(
"" => 'emptyd',
),
/*9*/ 'empty single quotes' => array(
'' => 'emptys',
),
// string data
/*10*/ 'string' => array(
"stringd" => 'stringd',
'strings' => 'strings',
$heredoc => 'stringh',
),
// undefined data
/*11*/ 'undefined' => array(
@$undefined_var => 'undefined',
),
// unset data
/*12*/ 'unset' => array(
@$unset_var => 'unset',
),
);
// loop through each element of
// $inputs to check the behavior of array_merge
$iterator = 1;
foreach($inputs as $key => $input) {
echo "<br><br>Iteration $iterator: $key data<br><br>";
var_dump( array_merge($input, $arr) );
var_dump( array_merge($arr, $input) );
$iterator++;
};
?>
EXERCISE
<?php
/*
* Pass array_merge() arrays with
* only numeric keys to test behaviour.
* $arr2 contains a duplicate element to $arr1.
*/
echo "Testing usage variations:<br><br>";
//numeric keys
$arr1 = array('zero', 'one', 'two', 'three');
$arr2 = array(1 => 'one', 20 => 'twenty', 30 => 'thirty');
echo 'The given ARRAY(1)<br>';
print_r($arr1);
echo '<br><br>The given ARRAY(2)<br>';
print_r($arr2);
echo '<br><br>Merging 1 to 2:<br>';
var_dump(array_merge($arr1, $arr2));
echo '<br><br>Merging 2 to 1:<br>';
var_dump(array_merge($arr2, $arr1));
?>
EXERCISE
<?php
/*
* Pass array_merge arrays with
* string keys to test behaviour.
* $arr2 has a duplicate key to $arr1
*/
echo "Testing usage variations:<br><br>";
//string keys
$arr1 = array('zero' => 'zero', 'one' => 'un', 'two' => 'deux');
$arr2 = array('zero' => 'zero', 'un' => 'eins', 'deux' => 'zwei');
echo 'The given ARRAY(1)<br>';
print_r($arr1);
echo '<br><br>The given ARRAY(2)<br>';
print_r($arr2);
echo '<br><br>Merging 1 to 2:<br>';
var_dump(array_merge($arr1, $arr2));
echo '<br><br>Merging 2 to 1:<br>';
var_dump(array_merge($arr2, $arr1));
?>
EXERCISE
<?php
/*
* Pass array_merge() arrays
* with mixed keys to test how it
* attaches them to existing arrays
*/
echo "Testing usage variations:<br><br>";
//mixed keys
$arr1 = array('zero', 20 => 'twenty', 'thirty' => 30, true => 'bool');
$arr2 = array(0, 1, 2, null => 'null', 1.234E-10 => 'float');
var_dump(array_merge($arr1, $arr2));
var_dump(array_merge($arr2, $arr1));
echo 'The given ARRAY(1)<br>';
print_r($arr1);
echo '<br><br>The given ARRAY(2)<br>';
print_r($arr2);
echo '<br><br>Merging 1 to 2:<br>';
var_dump(array_merge($arr1, $arr2));
echo '<br><br>Merging 2 to 1:<br>';
var_dump(array_merge($arr2, $arr1));
?>
EXERCISE
<?php
/*
* Test array_merge() with
* multi-dimensional arrays
*/
echo "Testing usage variations:<br><br>";
$arr1 = array('zero', 'one', 'two', array(0));
$arr2 = array(1, 2, 3);
echo 'The given ARRAY(1)<br>';
print_r($arr1);
echo '<br><br>The given ARRAY(2)<br>';
print_r($arr2);
echo "<br><br>Merge a two-dimensional and a one-dimensional array:<br><br>";
var_dump(array_merge($arr1, $arr2));
echo "<br><br>Merge an array and a sub-array:<br>";
var_dump(array_merge($arr1[3], $arr2));
echo '<br><br>';
var_dump(array_merge($arr2, $arr1[3]));
?>
EXERCISE
<?php
/* Test array_merge() when:
* 1. Passed an array made up of
* referenced variables
* 2. Passed an array as the first argument
* and a reference to that array as the second.
*/
echo "Testing usage variations:<br><br>";
$val1 = 'foo';
$val2 = 'bar';
$val3 = 'baz';
$arr1 = array(&$val1, &$val2, &$val3);
$arr2 = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
echo 'The given ARRAY(1)<br>';
print_r($arr1);
echo '<br><br>The given ARRAY(2)<br>';
print_r($arr2);
echo "<br><br>Merge an array made up of<br>
referenced variables to an associative array:<br>";
var_dump(array_merge($arr1, $arr2));
echo '<br><br>';
var_dump(array_merge($arr2, $arr1));
$val2 = 'hello world';
echo "<br><br>Change \$val2:<br>";
var_dump(array_merge($arr1, $arr2));
echo '<br><br>';
var_dump(array_merge($arr2, $arr1));
?>