Current Location: Home> Latest Articles> What is the difference between array_diff_key() and array_diff()?

What is the difference between array_diff_key() and array_diff()?

M66 2025-05-12

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

Function prototype:

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

Parameter description:

  • $array1 : The first array to be compared.

  • $array2 : The second array to compare.

  • $arrays (optional): More arrays for comparison.

Return value: Returns an array of key-value pairs that are included in $array1 but not in other arrays.

Example:

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

$array2 = [
    'a' => 1,
    'c' => 3,
    'd' => 4,
];

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

Output:

 Array
(
    [b] => 2
)

Explanation: In this example, array_diff_key() compares the keys in $array1 and $array2 . Only the key 'b' exists in $array1 , but not in $array2 , so the return result is an array containing only the key 'b' .

Introduction to array_diff()

array_diff() is used to compare values ​​of an array. It returns an array containing values ​​that exist in the first array but do not appear in other arrays.

Function prototype:

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

Parameter description:

  • $array1 : The first array to be compared.

  • $array2 : The second array to compare.

  • $arrays (optional): More arrays for comparison.

Return Value: Returns an array of values ​​that are included in $array1 but not in other arrays.

Example:

 $array1 = [1, 2, 3];
$array2 = [2, 3, 4];

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

Output:

 Array
(
    [0] => 1
)

Explanation: In this example, array_diff() compares the values ​​in $array1 and $array2 . The value 1 exists in $array1 , but is not in $array2 , so an array containing 1 is returned.

The main differences between array_diff_key() and array_diff()

  1. The objects compared are different:

    • array_diff_key() compares the keys of an array, that is, only pay attention to whether the keys of the array exist in other arrays.

    • array_diff() compares the value of the array, that is, only pays attention to whether the value of the array exists in other arrays.

  2. Different usage scenarios:

    • array_diff_key() is suitable for cases where arrays need to be filtered by keys. For example, you might want to find a key that is unique to an array.

    • array_diff() is suitable for cases where arrays need to be filtered based on values. For example, you might want to find elements that are unique to an array.

  3. The returned results are different:

    • array_diff_key() returns an array containing unique keys, and the array in the result retains the keys of the original array.

    • array_diff() returns an array containing unique values, and the keys of the array in the result will be re-indexed.

How to choose?

Choosing to use array_diff_key() or array_diff() mainly depends on whether you want to compare based on key or value:

  • Use array_diff_key() : When you only care about the keys of the array, use array_diff_key() to compare the keys.

    • For example: you have an array of user information and you need to look for fields (keys) that exist in that array but do not exist in other arrays.

  • Use array_diff() : When you only care about the values ​​of the array, use array_diff() to compare values.

    • For example: You have an order array and need to find out items that exist in it but are not in other orders.

Summarize

  • array_diff_key() compares keys, which are suitable for filtering arrays by keys.

  • array_diff() compares values ​​and is suitable for filtering arrays based on values.

  • Selecting the right function according to the specific scenario can help you process array data more efficiently.