Merge Two Arrays Keeping Original Keys in PHP

Merging two arrays while preserving their original keys is a common task in PHP.

It allows you to combine data from multiple arrays without reindexing the elements.

In this article, we'll explore different methods to merge two arrays while retaining their original keys in PHP.

Method 1: Using the + Operator

One straightforward way to merge two arrays while preserving their original keys is by using the + operator.

This operator combines two arrays, and if there are duplicate keys, it retains the value from the left-hand array (the first array).

Here's an example:

$array1 = ["a" => 1, "b" => 2, "c" => 3];
$array2 = ["b" => 4, "d" => 5, "e" => 6];

$result = $array1 + $array2;

print_r($result);

Output:

Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [d] => 5
    [e] => 6
)

In this example, we have two arrays, $array1 and $array2, with some overlapping keys.

When we use the + operator to merge them, it retains the values from $array1 for keys "a," "b," and "c" and adds the values from $array2 for keys "d" and "e."


Method 2: Using the array_merge() Function

The array_merge() function in PHP can also be used to merge two arrays while preserving their original keys.

This function merges two or more arrays into a new array, and it retains the keys and values of all the arrays.

Here's an example:

$array1 = ["a" => 1, "b" => 2, "c" => 3];
$array2 = ["b" => 4, "d" => 5, "e" => 6];

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

print_r($result);

Output:

Array
(
    [a] => 1
    [b] => 4
    [c] => 3
    [d] => 5
    [e] => 6
)

In this example, array_merge($array1, $array2) combines the two arrays into a new array while preserving their original keys.

When there are duplicate keys, it retains the value from the last array (in this case, $array2).


Method 3: Using the array_replace() Function

The array_replace() function is another option for merging arrays while keeping their original keys.

This function replaces the values of the first array with the values from the following arrays, and it also preserves the keys.

Here's an example:

$array1 = ["a" => 1, "b" => 2, "c" => 3];
$array2 = ["b" => 4, "d" => 5, "e" => 6];

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

print_r($result);

Output:

Array
(
    [a] => 1
    [b] => 4
    [c] => 3
    [d] => 5
    [e] => 6
)

In this example, array_replace($array1, $array2) combines the two arrays into a new array while preserving their original keys.

Similar to array_merge(), when there are duplicate keys, it retains the value from the last array (in this case, $array2).

Conclusion

Merging two arrays while retaining their original keys is a common operation in PHP. You can choose the method that best suits your requirements.

Whether you prefer using the + operator for simplicity, array_merge() for more control, or array_replace() for value replacement, these methods enable you to combine data from multiple arrays while preserving the structure and keys of the original arrays.