Logo

How to Merge Two Arrays in PHP?

How to Merge Two Arrays in PHP?

How to Merge Two Arrays in PHP?

Hello, developers! Merging arrays is a common task in PHP, whether you’re combining data from different sources or merging configurations.

In this article, we’ll explore all possible methods to merge two arrays in PHP, providing examples and explanations for each method. Let’s dive in!

Method 1 : Using array_merge()

The array_merge() function is the most straightforward way to merge two or more arrays. It combines the arrays and re-indexes numeric keys.

Example 1 : Merging Arrays with array_merge()

In this example, array_merge() combines $array1 and $array2 into a single array.

				
					<?php

$array1 = ['apple', 'banana'];
$array2 = ['cherry', 'date'];

$result = array_merge($array1, $array2);

print_r($result);

?>

				
			

Output :

				
					Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => date
)

				
			

Example 2 : Merging Associative Arrays with array_merge()

When merging associative arrays, array_merge() overwrites values from the first array with values from subsequent arrays if the keys are the same.

				
					<?php

$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['b' => 'blueberry', 'c' => 'cherry'];

$result = array_merge($array1, $array2);

print_r($result);

?>

				
			

Output :

				
					Array
(
    [a] => apple
    [b] => blueberry
    [c] => cherry
)

				
			

Method 2 : Using array_merge_recursive()

The array_merge_recursive() function merges arrays recursively, combining values with the same keys into nested arrays.

Example 3 : Merging Arrays with array_merge_recursive()

In this example, array_merge() combines $array1 and $array2 into a single array.
In below example, array_merge_recursive() combines values with the same key into an array.

				
					<?php

$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['b' => 'blueberry', 'c' => 'cherry'];

$result = array_merge_recursive($array1, $array2);

print_r($result);

?>

				
			

Output :

				
					Array
(
    [a] => apple
    [b] => Array
        (
            [0] => banana
            [1] => blueberry
        )
    [c] => cherry
)

				
			

Method 3 : Using the + Operator

The + operator merges two arrays without re-indexing numeric keys. It only adds elements from the second array if they do not exist in the first array.

Example 4: Merging Arrays with the + Operator

The + operator merges two arrays without re-indexing numeric keys. It only adds elements from the second array if they do not exist in the first array.

Here, the + operator keeps the values from $array1 if keys are the same, ignoring the values from $array2.

				
					<?php

$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['b' => 'blueberry', 'c' => 'cherry'];

$result = $array1 + $array2;

print_r($result);

?>

				
			

Output :

				
					Array
(
    [a] => apple
    [b] => banana
    [c] => cherry
)

				
			

Method 4: Using array_replace()

The array_replace() function replaces values in the first array with values from subsequent arrays if keys are the same.

Example 5 : Merging Arrays with array_replace()

In this example, array_replace() overwrites values from $array1 with values from $array2 if the keys are the same.

				
					<?php

$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['b' => 'blueberry', 'c' => 'cherry'];

$result = array_replace($array1, $array2);

print_r($result);

?>

				
			

Output :

				
					Array
(
    [a] => apple
    [b] => blueberry
    [c] => cherry
)

				
			

Conclusion :

Merging arrays in PHP is a versatile task with multiple methods available depending on your needs. Whether you use array_merge(), array_merge_recursive(), the + operator, or array_replace(), each method has its specific use case. Understanding these methods will help you handle arrays more effectively in your PHP projects.

We hope this article has provided you with a comprehensive guide to merging arrays in PHP. Happy coding!

Scroll to Top