The Wayback Machine - https://web.archive.org/web/20240619123251/https://www.geeksforgeeks.org/merge-two-arrays-keeping-original-keys-in-php/
Open In App

Merge two arrays keeping original keys in PHP

Last Updated : 07 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
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] => ...

Previous Article
Next Article

Similar Reads

Javascript Program For Partitioning A Linked List Around A Given Value And Keeping The Original Order
Given a linked list and a value x, partition it such that all nodes less than x come first, then all nodes with a value equal to x, and finally nodes with a value greater than or equal to x. The original relative order of the nodes in each of the three partitions should be preserved. The partition must work in place.Examples: Input: 1-&gt;4-&gt;3-
4 min read
How to merge arrays and preserve the keys in PHP ?
Arrays in PHP are created using array() function. Arrays are variable that can hold more than one values at a time. There are three types of arrays: Indexed Arrays Associative Arrays Multidimensional Arrays Each and every value in an array has a name or identity attached to it used to access that element called keys. To merge the two arrays array_m
2 min read
PHP Program to Merge two Sorted Arrays
Given two sorted arrays, i.e. arr1, and arr2, the task is to merge both sorted arrays and make a single sorted array. Examples: Input: arr1 = [1, 3, 5, 7, 8] arr2 = [2, 3, 4, 5, 6]Output: [1, 2, 3, 3, 4, 5, 6, 7, 8]There are two methods to merge both arrays, these are: Table of Content Using LoopUsing array_merge() FunctionUsing LoopHere, we iterat
2 min read
Merge Two Arrays & Remove Duplicate Items in PHP
Given two arrays, the task is to merge both arrays and remove the duplicate elements from from merged array in PHP. Below are the approaches to merge two arrays and remove duplicate items in PHP: Table of Content Using array_merge() and array_unique() FunctionUsing array_merge() with array_flip() FunctionUsing array_merge() with array_values() and
3 min read
How to create 2 column layout while keeping column background colors full size ?
In this article, we will learn how to make two-column layouts while keeping column background colors full size. Approach: We can make a two-column layout with background colors occupying full size by using the flexbox technique in CSS and by setting the height of the columns with respect to the viewport. HTML code: The following code demonstrates t
1 min read
How to Merge two Different Arrays of Objects with Unique Values only in JavaScript ?
Merging two arrays of objects with unique values is a common task in JavaScript. This operation involves combining the elements of two arrays while ensuring that each resulting object has a unique identifier. We will explore various approaches to merging two arrays of objects with unique values. These are the following approaches: Table of Content
4 min read
JavaScript Program to Merge two Sorted Arrays into a Single Sorted Array
In this article, we will cover how to merge two sorted arrays into a single sorted array in JavaScript. Given there are two sorted arrays, we need to merge them into a single sorted array in JavaScript. We will see the code for each approach along with the output. Below are the possible approaches that will be discussed in this article: Approaches
3 min read
How to merge two arrays and remove duplicate items in JavaScript ?
Merging two arrays while eliminating duplicate items in JavaScript involves combining the arrays into a single array and then removing any duplicate elements. This ensures that the resulting array contains only unique items from both original arrays, facilitating efficient data management and manipulation. There are some methods to merge two arrays
3 min read
JavaScript Program to Merge Two Arrays Without Creating a New Array
In this article, we will learn how to merge two arrays without creating a new array. In JavaScript Array is used to store elements of similar data types. If we want to add an element into another array for merging two arrays, we can use the push method. There are many methods for merging two arrays without creating a new array. Table of Content Met
2 min read
How to merge two PHP objects?
Given two objects of same class and the task is to merge both objects into single object.Approach 1: Convert object into data array and merge them using array_merge() function and convert this merged array back into object of class stdClass.Note: While merging the objects using array_merge(), elements of array in argument1 are overwritten by elemen
3 min read