Current Location: Home> Latest Articles> Note: array_diff_key() only compares key names, not key values

Note: array_diff_key() only compares key names, not key values

M66 2025-05-15

In PHP, array operations are very common tasks, and there are many built-in functions that help us process arrays efficiently. array_diff_key() is a very practical function that helps us compare arrays based on key names and return arrays of differences. Many times, developers will have some misunderstandings about this function, believing that it will compare the values ​​corresponding to the key names. In fact, it will only compare the key names of the array, but not the key values . Today we will analyze the usage of this function in detail and clarify some common misunderstandings.

1. Function introduction

The array_diff_key() function is used to compare two (or more) arrays and returns an element containing the key names in the first array that do not appear in other arrays. That is, it returns a difference set containing key names that exist in the first array but not in the other arrays.

The function prototype is as follows:

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

  • $array2, ...$arrays : an array that needs to be compared with $array1 (multiple arrays can be passed in).

2. Sample code

To understand this function more clearly, let's look at an actual code example:

 <?php
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];

$array2 = [
    'b' => 5,
    'c' => 3,
    'd' => 7
];

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

The output result is:

 Array
(
    [a] => 1
)

In this example, the array_diff_key() function compares the key names in $array1 and $array2 . It returns a new array containing the key name 'a' because 'a' does not have a corresponding key in $array2 .

3. Key points: Only compare key names, not key values

An important feature of the array_diff_key() function is that it only compares the key names , but does not consider the corresponding values ​​of the key names. In the example above, we see that the key names 'b' and 'c' appear in both arrays, but their values ​​are different: the value corresponding to 'b' in $array1 is 2 , and the value corresponding to 'b' in $array2 is 5 . However, array_diff_key() does not compare values, it only compares key names, so neither 'b' nor 'c' appears in the return result.

4. Comparison of multiple arrays

array_diff_key() can also accept multiple arrays as parameters to compare key names in multiple arrays. Here is an example containing multiple arrays:

 <?php
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];

$array2 = [
    'b' => 5,
    'c' => 3,
    'd' => 7
];

$array3 = [
    'c' => 3,
    'd' => 7,
    'e' => 8
];

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

The output result is:

 Array
(
    [a] => 1
)

Here, array_diff_key() compares the key names of $array1 and $array2 and $array3 . The result returned is still only the key name 'a' , because 'b' and 'c' both find the corresponding key names in other arrays.

5. Things to note

  • Compare key names only : As mentioned earlier, array_diff_key() will only compare key names and will not compare key values.

  • Multiple array comparison : You can pass multiple arrays, array_diff_key() will return the unique key name in the first array based on the key names in all arrays.

  • The original array remains unchanged : This function does not modify the original array, but returns a new array.

6. Common misunderstandings

Sometimes, developers may mistakenly think that array_diff_key() will compare the key values ​​of an array. For example, the following code:

 <?php
$array1 = [
    'a' => 10,
    'b' => 20,
    'c' => 30
];

$array2 = [
    'a' => 10,
    'b' => 25,
    'c' => 30
];

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

Output result:

 Array
(
)

In this example, although the values ​​of key 'b' in arrays $array1 and $array2 are different ( 20 and 25 respectively), array_diff_key() returns an empty array because it only compares the key names, and the key names 'a' and 'c' are in both arrays, so they are not returned.

7. Summary

array_diff_key() is a very useful PHP function that helps us easily compare key names of arrays to find those that exist in the first array but not in other arrays. Remember, it only compares key names, not key values . This feature is especially useful when dealing with multidimensional arrays or when you need to filter data by only the key name.

Additional Reading