Current Location: Home> Latest Articles> What happens if there are duplicate keys in the comparison array?

What happens if there are duplicate keys in the comparison array?

M66 2025-06-06

What happens if there are duplicate keys in the array when using the array_diff_key function? What is the result?

In PHP, the array_diff_key() function is used to compare two or more arrays and return an array containing key names that exist in the first array but not in other arrays. How does the function work if the key names are repeated? Let's take a look at the specific situation.

Function introduction

The basic syntax of the array_diff_key() function is as follows:

 array_diff_key(array $array1, array $array2, array ...$arrays): array
  • $array1 : The first array to be compared.

  • $array2, ...$arrays : Other arrays that need to be compared with the first array.

This function returns an array containing the key names present in the first array $array1 , while elements of these key names cannot be found in the subsequent array $array2 , ... $arrays .

Key duplication problem when using array_diff_key()

The behavior of array_diff_key() can be surprising when there are duplicate keys in the array. Array keys in PHP are unique, and even if you specify the same key to an array, only the last key-value pair will be preserved in the end.

Example 1: There are duplicate keys in the array

 $array1 = [
    "a" => 1,
    "b" => 2,
    "c" => 3,
    "a" => 4  // Repeat keys
];

$array2 = [
    "a" => 100,
    "b" => 200
];

$result = array_diff_key($array1, $array2);
print_r($result);

In this example, the $array1 middle key "a" is defined twice, starting with 1 and then overridden by 4 . So $array1 actually has only one key "a" => 4 .

After running the above code, the output will be:

 Array
(
    [c] => 3
)

array_diff_key() will compare array $array1 and $array2 and return the element corresponding to the key name "c" because there is no "c" key in $array2 .

Example 2: Comparison of multiple arrays

 $array1 = [
    "a" => 1,
    "b" => 2,
    "c" => 3,
    "a" => 4  // Repeat keys
];

$array2 = [
    "a" => 100,
    "b" => 200
];

$array3 = [
    "c" => 300,
    "d" => 400
];

$result = array_diff_key($array1, $array2, $array3);
print_r($result);

In this example, the key "a" in $array1 is repeatedly defined as 1 and 4 , but will eventually be retained as 4 . The function will compare array1 , array2 and array3 , and return elements corresponding to the key name "d" because "d" is unique in $array1 and not in other arrays.

The output result is as follows:

 Array
(
    [d] => 400
)

Results Analysis

  • Effects of duplicate keys : When there are duplicate keys in the array, PHP will automatically overwrite the previous key-value pairs, leaving only the last key-value pair. Therefore, array_diff_key() will be compared by the final key name, ignoring the value of the intermediate duplicates.

  • Uniqueness of key names : array_diff_key() relies on the uniqueness of key names for comparison. If a key in the array exists in multiple arrays, the final reserved key will be based on the value of the key in the last occurrence of the array.

Summarize

When using the array_diff_key() function, the duplicate keys in the array will be automatically deduplicated, and only the last key-value pair will be valid. If you need to keep multiple duplicate keys, consider using other methods, such as the associative array of key-value pairs as values.

Hopefully this article helps you better understand the behavior of the array_diff_key() function, especially when dealing with repeated keys in an array.

URL domain name replacement in code

Please note that the code example in the article does not involve specific URLs, but if similar URL usage is involved in the actual development process, you can replace the domain name in the URL with m66.net as required, for example: