PHP array_merge Function

The array_merge function in PHP is used to merge one or more arrays into a single array. It takes multiple arrays as arguments, and it returns a new array containing the elements of all the input arrays. If two or more input arrays have the same string keys, the later value will overwrite the previous one.

In summary, the array_merge function merges associative arrays, and when there are duplicate keys, the values from the second array overwrite the values from the first array.

Example 1:

 
$students1 = [
  "John" => 25,
  "Jane" => 23,
];
 
$students2 = [
  "David" => 27,
  "Emily" => 22,
];
 
$mergedStudents = array_merge($students1, $students2);
 
print_r($mergedStudents);
Output:
 

Array
(
    [John] => 25
    [Jane] => 23
    [David] => 27
    [Emily] => 22
)

Explanation:

  • $students1 and $students2 contain student names and ages.
  • Merging them with array_merge() combines both sets into $mergedStudents.
  • "John" and "Jane" from $students1 are preserved.
  • "David" and "Emily" from $students2 are added at the end.

 

Example 2:

 
$electronics = ['TV' => 'Sony', 'Laptop' => 'Apple', 'TV' => 'LG'];
$appliances = ['Fridge' => 'Samsung', 'TV' => 'Panasonic', 'Stove' => 'Bosch'];
 
$combinedProducts = array_merge($electronics, $appliances);
 
print_r($combinedProducts);
 
Result:
 

Array
(
    'TV' => 'LG', // Last value for "TV" overwrites the first
    'Laptop' => 'Apple',
    'Fridge' => 'Samsung',
    'Stove' => 'Bosch',
)

 

The merged array $combinedProducts contains the combined information from both $electronics and $appliances. Notice that the value for the key 'TV' in $combinedProducts is taken from the second array ($appliances), and the value 'LG' from $electronics is overwritten.

In summary, the array_merge function merges associative arrays, and when there are duplicate keys, the values from the second array overwrite the values from the first array. In this example, the merged array $combinedProducts contains information about electronics and appliances, with values for 'TV' and other keys taken from the second array ($appliances).

 

Example 3:

 
$california = ['UCLA', 'USC', 'Stanford'];
$texas = ['UT Austin', 'Texas A&M', ['Rice', 'Baylor']];
 
$allColleges = array_merge($california, $texas);
 
print_r($allColleges);
 
Result:
 

Array
(
    [0] => UCLA
    [1] => USC
    [2] => Stanford
    [3] => UT Austin
    [4] => Texas A&M
    [5] => Array
        (
            [0] => Rice
            [1] => Baylor
        )
)

The merged array $allColleges contains elements from both California and Texas arrays. The colleges from $texas are added to the end of the $california array. The nested array within $texas is preserved as an element in the merged array.

In summary, the array_merge function is used to combine two arrays ($california and $texas), resulting in a new array ($allColleges) that contains all the colleges from both California and Texas. The nested array within $texas is treated as a single element in the merged array.