Open In App

Merge two arrays keeping original keys in PHP

Last Updated : 09 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Merging two arrays in PHP while preserving the original keys can be done using the array_merge or array_replace functions, depending on the desired behavior.

Below are the methods to merge two arrays while keeping the original keys in PHP:

Using + operator

The + operator preserves the keys of the first array and does not overwrite them with values from the second array. This is useful when you want to keep the original keys and values from the first array and only add new keys from the second array.

Example: This example shows the use of the above-explained approach.

php
<?php
$array1 = array(
         1 =>'Geeks',
         2 =>'For',
         3 =>'Geeks'
          );

 $array2 = array(
          4 => 'A',
          5 => 'Computer',
          6 => 'Science',
          7 => 'Portal',
          8 => 'For',
          9 => 'Geeks'
         );

  $merged_array = $array1 + $array2;
  var_dump ($merged_array);
?>

Output
array(9) {
  [1]=>
  string(5) "Geeks"
  [2]=>
  string(3) "For"
  [3]=>
  string(5) "Geeks"
  [4]=>
  string(1) "A"
  [5]=>
  string(8) "Computer"
  [6]=>
  string(7) "Science"
  [7]=>
  string(6) "Portal"
  [8]=>
  string(3) "For"
  [9]=>
  string(5) "Geeks"
}

Using inbuilt array_replace() function

The array_replace replaces values in the first array with values from the second array when keys are the same.

Example: This example shows the use of the above-explained approach.

PHP
<?php
$array1 = array(
         1 =>'Geeks',
         2 =>'For',
         3 =>'Geeks'
          );

 $array2 = array(
          4 => 'A',
          5 => 'Computer',
          6 => 'Science',
          7 => 'Portal',
          8 => 'For',
          9 => 'Geeks'
         );

  $merged_array = array_replace($array1, $array2);
  var_dump ($merged_array);
?>



Output
array(9) {
  [1]=>
  string(5) "Geeks"
  [2]=>
  string(3) "For"
  [3]=>
  string(5) "Geeks"
  [4]=>
  string(1) "A"
  [5]=>
  string(8) "Computer"
  [6]=>
  string(7) "Science"
  [7]=>
  string(6) "P...

Using foreach loop

To merge two arrays while preserving original keys using a foreach loop in PHP, iterate through the second array and add each key-value pair to the first array. This way, the original keys from both arrays are maintained in the merged array

Example: This example shows the use of the above-explained approach.

PHP
$array1 = ['a' => 'Apple', 'b' => 'Banana'];
$array2 = ['c' => 'Cherry', 'd' => 'Date'];
foreach($array2 as $key => $value) {
    $array1[$key] = $value;
}
print_r($array1);
// Output: Array ( [a] => Apple [b] => Banana [c] => Cherry [d] => Date )

Output
$array1 = ['a' => 'Apple', 'b' => 'Banana'];
$array2 = ['c' => 'Cherry', 'd' => 'Date'];
foreach($array2 as $key => $value) {
    $array1[$key] = $value;
}
print_r($array1);
// Output: Array ( [a] => ...

Using array_merge

The PHP code merges two arrays, $array1 and $array2, using array_merge(). It combines their elements while preserving original keys. If keys overlap, values from $array2 overwrite those from $array1.

Example: In this example we are following above explained approach.

PHP
<?php
// First array
$array1 = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];

// Second array
$array2 = ['b' => 'orange', 'd' => 'date'];

// Merge arrays while preserving original keys
$mergedArray = array_merge($array1, $array2);

// Print the merged array
print_r($mergedArray);
?>

Output
Array
(
    [a] => apple
    [b] => orange
     => cherry
    [d] => date
)

Using array_replace_recursive() Function

The array_replace_recursive function replaces values in the first array with values from the second array when keys are the same. This function works recursively for nested arrays, ensuring that all levels of the arrays are merged correctly.

Example: In this example we merges two arrays recursively, replacing values from $array1 with values from $array2 where keys overlap, resulting in a combined array with updated nested values.

PHP
<?php

$array1 = ['a' => 1, 'b' => ['x' => 10, 'y' => 20]];
$array2 = ['b' => ['y' => 30, 'z' => 40], 'c' => 4];

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

print_r($result);

?>

Output
Array
(
    [a] => 1
    [b] => Array
        (
            [x] => 10
            [y] => 30
            [z] => 40
        )

     => 4
)


Next Article

Similar Reads