Syntax
array_merge(array $array1, array $array2, ...): array
Parameters
- $array1, $array2, ...: These are the arrays you want to merge. You can pass two or more arrays.
Return Value
The function returns a new array containing the merged elements of the input arrays.
Example
Here’s a simple example to demonstrate the array_merge() function:
<?php
// Define two arrays
$array1 = ["apple", "banana"];
$array2 = ["cherry", "date"];
// Merge the arrays
$result = array_merge($array1, $array2);
// Print the merged array
print_r($result);
?>

You can also try this code with Online PHP Compiler
Run CodeOutput:
Array
(
[0] => apple
[1] => banana
[2] => cherry
[3] => date
)
Explanation:
- The elements from $array2 are appended to $array1.
- The keys are renumbered sequentially.
Parameter Values
The array_merge() function supports multiple arrays as arguments. Let’s understand this with more clarity:
Numeric Keys
If the input arrays have numeric keys, they will be renumbered in the resulting array:
<?php
$array1 = [10 => "apple", 20 => "banana"];
$array2 = ["cherry", "date"];
$result = array_merge($array1, $array2);
print_r($result);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
[2] => cherry
[3] => date
)
Explanation:
- The numeric keys from $array1 are ignored, and the values are renumbered sequentially.
String Keys
For arrays with string keys, the values of duplicate keys are overwritten by later arrays:
<?php
$array1 = ["fruit" => "apple", "color" => "red"];
$array2 = ["color" => "green", "taste" => "sweet"];
$result = array_merge($array1, $array2);
print_r($result);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[fruit] => apple
[color] => green
[taste] => sweet
)
Explanation:
- The color key is present in both arrays. The value from $array2 (green) overwrites the value from $array1 (red).
Merging Two Simple Arrays
Merging two arrays in PHP is simple. PHP provides a built-in function called `array_merge()` that allows you to combine two or more arrays into a single array. This function takes the arrays as input & returns a new array that contains all the elements from the input arrays.
Let’s see how it works:
1. Syntax of `array_merge()`:
array_merge(array1, array2, array3, ...);
- `array1`, `array2`, `array3`, etc., are the arrays you want to merge.
- The function returns a new array containing all the elements from the input arrays.
2. Example:
Let’s say we have two arrays:
$array1 = array("apple", "banana");
$array2 = array("orange", "grapes");

You can also try this code with Online PHP Compiler
Run Code
We want to merge these two arrays into one. Here’s how you can do it:
$mergedArray = array_merge($array1, $array2);
print_r($mergedArray);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grapes
)
3. Explanation:
- The `array_merge()` function takes `$array1` & `$array2` as input.
- It combines the elements of both arrays into a new array called `$mergedArray`.
- The `print_r()` function is used to display the contents of the merged array.
4. What Happens If Arrays Have the Same Keys?
If the arrays have the same string keys, the value from the later array will overwrite the value from the earlier array. For example:
$array1 = array("fruit1" => "apple", "fruit2" => "banana");
$array2 = array("fruit2" => "orange", "fruit3" => "grapes");
$mergedArray = array_merge($array1, $array2);
print_r($mergedArray);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[fruit1] => apple
[fruit2] => orange
[fruit3] => grapes
)
Here, the key `fruit2` in `$array2` overwrites the value of `fruit2` in `$array1`.
5. Merging More Than Two Arrays:
You can merge as many arrays as you want using `array_merge()`. For example:
$array1 = array("apple", "banana");
$array2 = array("orange", "grapes");
$array3 = array("mango", "pineapple");
$mergedArray = array_merge($array1, $array2, $array3);
print_r($mergedArray);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grapes
[4] => mango
[5] => pineapple
)
More Examples
Merging Arrays with Mixed Keys
<?php
$array1 = ["apple", "banana", "key1" => "cherry"];
$array2 = ["key1" => "date", "grape"];
$result = array_merge($array1, $array2);
print_r($result);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
[key1] => date
[2] => grape
)
Explanation:
- The key1 from $array2 overwrites the key1 from $array1.
- Numeric values from $array2 are appended.
Using array_merge() with Empty Arrays
<?php
$array1 = [];
$array2 = ["apple", "banana"];
$result = array_merge($array1, $array2);
print_r($result);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
)
Explanation:
- An empty array doesn’t affect the merging process.
Merging Multiple Arrays
<?php
$array1 = ["apple", "banana"];
$array2 = ["cherry", "date"];
$array3 = ["grape", "fig"];
$result = array_merge($array1, $array2, $array3);
print_r($result);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
[2] => cherry
[3] => date
[4] => grape
[5] => fig
)
Explanation:
- The function combines all three arrays into a single array.
Union Operator
In PHP, the union operator (`+`) is another way to combine arrays. Unlike `array_merge()`, the union operator does not overwrite values if the keys are the same. Instead, it keeps the value from the first array & ignores the value from the second array if the key already exists.
Let’s see how it works:
1. Syntax of the Union Operator:''
$resultArray = $array1 + $array2;
- `$array1` & `$array2` are the arrays you want to combine.
- The union operator (`+`) returns a new array containing elements from both arrays, but it prioritizes the values from the first array if keys overlap.
2. Example:
Let’s say we have two arrays:
$array1 = array("fruit1" => "apple", "fruit2" => "banana");
$array2 = array("fruit2" => "orange", "fruit3" => "grapes");

You can also try this code with Online PHP Compiler
Run Code
We want to combine these two arrays using the union operator. Here’s how you can do it:
$resultArray = $array1 + $array2;
print_r($resultArray);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[fruit1] => apple
[fruit2] => banana
[fruit3] => grapes
)
3. Explanation:
- The union operator (`+`) combines `$array1` & `$array2`.
- Since both arrays have the key `fruit2`, the value from `$array1` (`banana`) is kept, & the value from `$array2` (`orange`) is ignored.
- The key `fruit3` from `$array2` is added to the result because it doesn’t exist in `$array1`.
4. Comparison with `array_merge()`:
- The union operator (`+`) does not reindex numeric keys. If the arrays have numeric keys, the values from the first array are always kept.
- For example:
$array1 = array(0 => "apple", 1 => "banana");
$array2 = array(1 => "orange", 2 => "grapes");
$resultArray = $array1 + $array2;
print_r($resultArray);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
[2] => grapes
)
Here, the value `banana` from `$array1` is kept for the key `1`, & the value `orange` from `$array2` is ignored.
5. When to Use the Union Operator:
- Use the union operator when you want to combine arrays but prioritize the values from the first array.
- It’s particularly useful when working with associative arrays where you don’t want to overwrite existing keys.
Comparison of Arrays
When working with arrays in PHP, it’s often necessary to compare them to check if they are equal, identical, or have differences. PHP provides several functions to compare arrays, such as `==`, `===`, `array_diff()`, and `array_intersect()`. Let’s understand these methods in detail.
1. Equality Check (`==`)
The equality operator (`==`) checks if two arrays have the same key-value pairs, regardless of their order or data types.
Example:
$array1 = array("fruit1" => "apple", "fruit2" => "banana");
$array2 = array("fruit2" => "banana", "fruit1" => "apple");
if ($array1 == $array2) {
echo "The arrays are equal.";
} else {
echo "The arrays are not equal.";
}

You can also try this code with Online PHP Compiler
Run Code
Output:
The arrays are equal.
In this code:
Both arrays have the same key-value pairs, even though the order is different.
The `==` operator considers them equal because the content is the same.
2. Identity Check (`===`)
The identity operator (`===`) checks if two arrays have the same key-value pairs, in the same order, and of the same data types.
Example:
$array1 = array("fruit1" => "apple", "fruit2" => "banana");
$array2 = array("fruit1" => "apple", "fruit2" => "banana");
if ($array1 === $array2) {
echo "The arrays are identical.";
} else {
echo "The arrays are not identical.";
}

You can also try this code with Online PHP Compiler
Run Code
Output:
The arrays are identical.
In this code:
Both arrays have the same key-value pairs in the same order.
The `===` operator considers them identical because the content, order, and data types are the same.
3. Difference Between Arrays (`array_diff()`):
The `array_diff()` function compares arrays and returns the values from the first array that are not present in the other arrays.
Example:
$array1 = array("apple", "banana", "orange");
$array2 = array("banana", "grapes");
$difference = array_diff($array1, $array2);
print_r($difference);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[2] => orange
)
In this code:
The `array_diff()` function compares `$array1` with `$array2`.
It returns the values `apple` and `orange` because they are present in `$array1` but not in `$array2`.
4. Intersection of Arrays (`array_intersect()`)
The `array_intersect()` function compares arrays and returns the values that are present in all arrays.
Example:
$array1 = array("apple", "banana", "orange");
$array2 = array("banana", "grapes", "apple");
$intersection = array_intersect($array1, $array2);
print_r($intersection);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => apple
[1] => banana
)
In this code:
The `array_intersect()` function compares `$array1` with `$array2`.
It returns the values `apple` and `banana` because they are present in both arrays.
5. Key Comparison (`array_diff_assoc()`)
The `array_diff_assoc()` function compares arrays and returns the differences based on both keys and values.
Example:
$array1 = array("fruit1" => "apple", "fruit2" => "banana");
$array2 = array("fruit1" => "apple", "fruit3" => "grapes");
$difference = array_diff_assoc($array1, $array2);
print_r($difference);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[fruit2] => banana
)
In this code:
The `array_diff_assoc()` function compares `$array1` with `$array2`.
It returns the key-value pair `fruit2 => banana` because it exists in `$array1` but not in `$array2`.
Frequently Asked Questions
What happens if both arrays have string keys?
If the same string key exists in multiple arrays, the value from the later array overwrites the earlier one.
Can array_merge() be used with associative arrays?
Yes, it works seamlessly with associative arrays, preserving string keys and merging values as explained above.
Does array_merge() modify the original arrays?
No, it creates a new array and leaves the original arrays untouched.
Conclusion
The array_merge() function is a powerful tool for merging arrays in PHP. It simplifies data handling and is especially useful for combining configurations or datasets. By understanding how numeric and string keys are handled, you can use this function effectively in your projects.