Current Location: Home> Latest Articles> How is array_diff_key() different from array_diff_assoc()?

How is array_diff_key() different from array_diff_assoc()?

M66 2025-06-06

In PHP, array_diff_key() and array_diff_assoc() are both functions used to compare arrays, but their comparison methods are obviously different from those applicable scenarios. This article will explain the differences between these two functions in detail and discuss their respective usage scenarios.

1. array_diff_key() function

array_diff_key() is used to compare the key names (keys) of two or more arrays and returns those key-value pairs that exist in the first array but not in other arrays.

grammar

 array_diff_key(array $array1, array $array2, array ...$arrays): array

Parameter description

  • $array1 : The first array (reference array).

  • $array2, ...$arrays : The array to be compared with the first array.

Example

 $array1 = [
    "a" => 1,
    "b" => 2,
    "c" => 3
];

$array2 = [
    "a" => 4,
    "c" => 5
];

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

Output result

 Array
(
    [b] => 2
)

In this example, array_diff_key() compares the key names of two arrays. The results returned are those key-value pairs that are in $array1 but not in $array2 .

2. array_diff_assoc() function

array_diff_assoc() is used to compare key names and values ​​(key and value) of two or more arrays, returning key-value pairs that exist in the first array but not in other arrays. Unlike array_diff_key() , it not only compares the key names, but also compares the values ​​corresponding to the keys.

grammar

 array_diff_assoc(array $array1, array $array2, array ...$arrays): array

Parameter description

  • $array1 : The first array (reference array).

  • $array2, ...$arrays : The array to be compared with the first array.

Example

 $array1 = [
    "a" => 1,
    "b" => 2,
    "c" => 3
];

$array2 = [
    "a" => 4,
    "c" => 3
];

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

Output result

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

In this example, array_diff_assoc() compares the key names and values ​​of $array1 and $array2 . The results returned are those key-value pairs that have in $array1 but do not have the exact same key name and value in $array2 .

3. Difference between array_diff_key() and array_diff_assoc()

Key name vs Key value

  • array_diff_key() only compares the key names and ignores the corresponding values ​​of the key.

  • array_diff_assoc() compares key names and values ​​at the same time. Only when the key names and values ​​are different will it be considered different.

Use scenarios

  • array_diff_key() is suitable for scenarios where you only care about the key names of the array, but not the corresponding values. For example, you want to remove certain keys, or compare the structure of two arrays.

  • array_diff_assoc() is suitable for common scenarios when you need to completely compare keys and values ​​in an array, such as filtering out elements with different key names and values ​​in two arrays.

Example: URL replacement

Suppose we have an array that contains different URLs and needs to be replaced. We can use array_diff_key() or array_diff_assoc() to implement replacement, depending on whether we need to compare based on key or key value.

 $urls = [
    "home" => "http://m66.net/home",
    "about" => "http://example.com/about",
    "contact" => "http://m66.net/contact"
];

$other_urls = [
    "home" => "http://m66.net/home",
    "about" => "http://m66.net/about",
];

$result = array_diff_key($urls, $other_urls);
print_r($result);

Output result

 Array
(
    [contact] => http://m66.net/contact
)

In this example, we compare only based on the key name and finally find the items that are in $urls but not in $other_urls .

The above is the difference between array_diff_key() and array_diff_assoc() . According to your needs, choosing the right function to handle array differences can effectively improve the efficiency and readability of the code.