Difference between array_combine and array_merge in PHP

Summary:

The difference between the array_combine function & array_merge is that array_combine joins a keys array to a corresponding values array. In contrast, the array_merge function takes two or more arrays and merge them. They serve 2 different purposes.

Introduction

We have already seen array_combine and array_merge functions. While the function names suggest a similarity between the functions but both are different. In this article, we are going to explore the difference between array_combine and array_merge in PHP. Before diving straight into the topic, we suggest going through the posts for these functions.

PHP array_combine; How to use with examples in PHP

PHP array_merge : How to use with examples in PHP

To understand the difference, we will review these functions. The reviews will describe the functions precisely, and you will understand how these functions differ. So, let’s get into the main stuff.

What is the difference between array_combine and array_merge in PHP?

As we have mentioned already, we will review these functions here. The reviews will serve as a refresher on the topic and be helpful to elaborate on the array_merge and array_combine difference in PHP.

Difference between array_combine and array_merge: array_combine review

Here’s a review of the array_combine function.

Function Signature

array_combine(array $keys, array $values): array

Description

The array_combine function takes two arrays arguments $keys and $values. The function takes keys from the first array argument and values from the second array argument and combines them into an associative array with key and value pairs.

Difference between array_combine and array_merge

The figure above clarifies the array_combine further. It takes $keys array having the color names and $values array containing the corresponding hex codes. PHP array_combine joins them into an associative array with keys as color names and values as their hex codes.

Example

Here’s an example of the array_combine function. The function combines country codes as keys with their corresponding values of country names.

<?php
$keys = ["AU","BR","CA","US"];
$values = ["Australia","Brazil","Canada","United States"];
 
$combined_arr = array_combine($keys,$values);
 
print_r($combined_arr);
 
/*
OUTPUT
Array
(
    [AU] => Australia    
    [BR] => Brazil      
    [CA] => Canada      
    [US] => United States
)
*/
?>

Important caveats

We will list out the important caveats related to the array_combine function. If you’re determined to learn more about them, read our article about the function.

  • When there are duplicate keys, the function removes the redundancy and combines it with the value corresponding to the latest duplicate key.
  • If the count of $keys and $values differ, then the function throws an error.

So, we have revised the essential information about array_combine PHP. Next, we will review the array_merge function. These reviews clarify the difference between array_combine and array_merge in PHP.

Difference between array_combine and array_merge: array_merge review

Here’s a review of the array_merge function.

Function Signature

array_merge(array ...$arrays): array

Description

The array_merge function takes two or more two arrays and merges them. An important consideration while merging is to deal with duplicate key values. We will learn more about how the function deals with duplicate values in the following section: Important caveats.

Difference between array_combine and array_merge: array_merge

Example

Here’s an example of array_merge joining two arrays with numeric keys.

<?php

$array1 = [1,2,3,4,5];
$array2 = [6,7,8,9,10];
$array3 = [11,12,13,14,15];
$array4 = [16,17,18,19,20];
 
$merged_array = array_merge($array1, $array2, $array3, $array4);
 
print_r($merged_array);
 
/*
OUTPUT
Array      
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [
*/
?>

See, it reindexes the numeric keys. However, when the keys are strings, the function overwrites the previous value with the latest one.

<?php

$array1 = ["Emp#1"=>"Franklin","Emp#2"=>"Richard","Emp#3"=>"Jake"];
$array2 = ["Emp#4"=>"Tyson","Emp#2"=>"Narnie","Emp#5"=>"Kylie"];
 
$merged_array = array_merge($array1, $array2);
 
print_r($merged_array);
 
/*
OUTPUT
Array
(
    [Emp#1] => Franklin
    [Emp#2] => Narnie  
    [Emp#3] => Jake    
    [Emp#4] => Tyson  
    [Emp#5] => Kylie  
)
*/
?>

The identical key here is “Emp#2”, and the function assigns it the later value “Narnie” than the value “Richard”.

Important caveats

Based on the examples above, here are the important caveats related to the array_merge function.

  • The array_merge reindexes the numeric keys of the arrays. 
  • The array_merge overwrites the value of an identical string key with the latest value.

When to use array_combine and array_merge?

Array Mapping – array_combine use case

The array_combine is a handy function for mapping keys and values. We can use it for mapping two related records or data points. Here’s an example of mapping employees’ ids to their names.

<?php
$ids = ["E01", "E02", "E03", "E04", "E05"];
 
$names = ["Richard", "Sophie", "Diana", "David", "Kevin"];
 
$combined = array_combine($ids, $names);
 
print_r($combined);
 
/*
OUTPUT
Array
(
    [E01] => Richard
    [E02] => Sophie
    [E03] => Diana
    [E04] => David
    [E05] => Kevin
)
*/
?>

Voila! The array_combine works behind the scenes to precisely join the keys with values. Without this function, we would use a loop to iterate over the arrays and have logic for mapping. So, the function serves us big time.

Configuration settings – array_merge use case

Configuration files are an essential aspect of development. Developers usually tinker around the config files. Immatures find it daunting because they probably mess their projects up when unknowingly modifying things they shouldn’t. 

But let’s imagine a scenario with two configuration arrays, one with the old configuration settings and another with the latest ones. It is self-evident that the latest need to override the previous settings. So, we realise that we certainly would need a function like the array_merge to overwrite the values of similar keys. Here’s how.

<?php
$old_config = [
    "src" => '/project-src',
    "dest" => '/project-dest',
    "script" => 'run --start',
    "version" => '1.0',
    "author" => 'Alex'
];
 
$new_config = [
    "src" => '/project-src',
    "dest" => '/project-dest-new',
    "script" => 'run',
    "version" => '1.1',
    "author" => 'Alex'
];
 
$new_config = array_merge($old_config, $new_config);
 
print_r($new_config);
 
/*
OUTPUT
Array
(
    [src] => /project-src
    [dest] => /project-dest-new
    [script] => run
    [version] => 1.1
    [author] => Alex
)
*/

Voila! It doesn’t intermingle the config setting but overrides the old settings. That’s what we want.

Conclusion

In this article, we have seen array_merge and array_combine difference in PHP. We have reviewed these functions and included examples to clarify the behavioral differences between these two functions that set them apart. We hope that you’ve learned something new today. Stay tuned for more informative articles and tutorials related to PHP.

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

How to merge 2 arrays with the same keys in PHP

Difference between array_merge and array_merge_recursive in PHP

PHP array_filter: How to use with examples in PHP


Article Categories

Article Tags

© 2022 Confident.Systems