Current Location: Home> Latest Articles> If there are duplicate keys in the array, will array_combine() report an error?

If there are duplicate keys in the array, will array_combine() report an error?

M66 2025-05-16

In PHP, the array_combine() function is used to combine two arrays into an associative array. Its first parameter is the key array, and the second parameter is the value array. The function will use each element in the key array as the key to the associative array, and each element in the value array as the corresponding value, and finally return a new array.

However, if there are duplicate keys in the key array, then there will be problems when using array_combine() . Let's take a look at what happens and how to deal with this situation.

1. Basic usage of array_combine()

The syntax of array_combine() is as follows:

 array_combine(array $keys, array $values): array|false
  • $keys : an array containing key names.

  • $values : an array containing the corresponding values ​​of the key.

When the elements in the $keys array correspond to the elements in the $values ​​array one by one, array_combine() returns a new associative array.

Sample code:

 $keys = ['a', 'b', 'c'];
$values = [1, 2, 3];
$result = array_combine($keys, $values);
print_r($result);

Output result:

 Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

2. What happens when there are duplicate keys in the key array?

If there are duplicate keys in the key array, array_combine() will report an error, returning false . This is because in PHP, the keys of the array must be unique and cannot have duplicate key values.

Sample code:

 $keys = ['a', 'b', 'b'];
$values = [1, 2, 3];
$result = array_combine($keys, $values);

if ($result === false) {
    echo "An error occurred,Duplicate keys exist in the array。";
}

Output result:

 An error occurred,Duplicate keys exist in the array。

As shown above, array_combine() directly returns false and does not create a valid associative array.

3. How to deal with the situation where there are duplicate keys in the array?

To avoid errors caused by duplicate keys, you can check whether the key array contains duplicate keys before using array_combine() . If there are duplicate keys, you can usually take the following methods to deal with them:

3.1. Remove duplicate keys

The array_unique() function can be used to remove duplicate keys. This ensures that the key array passed into array_combine() is not duplicated.

 $keys = ['a', 'b', 'b'];
$values = [1, 2, 3];
$keys = array_unique($keys);  // Remove duplicate keys

$result = array_combine($keys, $values);
print_r($result);

Output result:

 Array
(
    [a] => 1
    [b] => 2
)
3.2. Custom bonding strategy

Another approach is to use a custom strategy to handle duplicate keys. For example, use array_count_values() to count the number of times each key appears, and process the repeated keys according to specific rules (for example, appending a numeric suffix).

 $keys = ['a', 'b', 'b'];
$values = [1, 2, 3];

$keys_count = array_count_values($keys);
foreach ($keys_count as $key => $count) {
    if ($count > 1) {
        // Processing duplicate keys,For example, append numbers after key
        $keys = array_map(function ($k) use ($key) {
            static $counter = 1;
            if ($k === $key) {
                return $key . $counter++;
            }
            return $k;
        }, $keys);
    }
}

$result = array_combine($keys, $values);
print_r($result);

Output result:

 Array
(
    [a] => 1
    [b1] => 2
    [b2] => 3
)

4. Summary

When using array_combine() , if there are duplicate keys in the key array, PHP will report an error and return false . To avoid this, you can use the array_unique() function to remove duplicate keys, or customize the strategy for handling duplicate keys according to your needs.

Through these methods, you can ensure that you avoid errors when using array_combine() , and adjust the handling of keys according to actual needs.