$result = array_merge($array1, $array2,...);
We will try with an example.
$first=array('a','b','c','d','e');
$second=array('f','g','h');
$result=array_merge($first,$second);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here
0 -> a
1 -> b
2 -> c
3 -> d
4 -> e
5 -> f
6 -> g
7 -> h
$first=array('a','b','c','d','e');
$second=array('h','d','b','f'); //with some common elements
$result=array_merge($first,$second);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here
0 -> a
1 -> b
2 -> c
3 -> d
4 -> e
5 -> h
6 -> d
7 -> b
8 -> f
$first=array(1=>'First',2=>'Second',3=>'Third');
$second=array(1=>'fourth',2=>'Fifth',3=>'Sixth');
$result=array_merge($first,$second);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
The output is here.
0 -> First
1 -> Second
2 -> Third
3 -> fourth
4 -> Fifth
5 -> Sixth
Keys are renumbered and incremented starting from 0.
$first=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple');
$second=array('new_fruit1'=> 'Strawberry','new_fruit2'=>'Grapes');
$result=array_merge($first,$second);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here
Fruits1 -> Banana
Fruits2 -> Mango
Fruits3 -> Apple
new_fruit1 -> Strawberry
new_fruit2 -> Grapes
$first=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple');
$second=array('Fruits1'=> 'Strawberry','Fruits2'=>'Grapes');
$result=array_merge($first,$second);
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here
Fruits1 -> Strawberry
Fruits2 -> Grapes
Fruits3 -> Apple
Keys of the first array will be retained.
$result=$first + $second;
If we use this in place of array_merge() ( in above code ) then the output will be like this
1 -> First
2 -> Second
3 -> Third
Let us change the second array and see the result.
$first=array(1=>'First',2=>'Second',3=>'Third');
$second=array(4=>'fourth',5=>'Fifth',6=>'Sixth');
$result=$first + $second;
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here
1 -> First
2 -> Second
3 -> Third
4 -> fourth
5 -> Fifth
6 -> Sixth
You can read more array Operators.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.