Understanding the PHP Function array_diff_key
The PHP function array_diff_key is an important tool for comparing arrays and identifying differences between them based on their keys. It's a simple but
array_diff_key() compares two or more arrays by their keys and returns the entries from the first array whose keys are not present in any of the other arrays. The key word is keys: the function never looks at the values. This makes it the right tool whenever you want to remove or keep array elements based on a set of keys — for example, stripping unwanted fields from form input or finding which configuration options are missing from an override.
This page covers the function signature, a runnable example, how it treats values and numeric keys, the most common real-world use cases, and how it differs from related array-comparison functions.
Syntax
array_diff_key(array $array, array ...$arrays): array$array— the array to compare against (the result only ever contains its entries)....$arrays— one or more arrays whose keys are used to exclude entries from$array.- Returns a new array. Each entry of
$arrayis kept only if its key appears in none of the other arrays. Keys and values are preserved exactly as they were in$array.
How array_diff_key Works
The function walks the keys of the first array. For each key, it checks every other array; if the key is found anywhere, that entry is dropped. Only entries whose keys are unique to the first array survive.
Here is a practical example — comparing a full price list against the items that already have a discount, to find the items that still need one:
PHP example of array_diff_key usage
<?php
$prices = [
"apple" => 1.20,
"banana" => 0.50,
"cherry" => 3.00,
"date" => 2.10,
];
// Items that already have a discounted price.
$discounted = ["banana" => 0.40, "date" => 1.90];
// Keep only the items NOT present in $discounted (compared by key).
$result = array_diff_key($prices, $discounted);
print_r($result);
?>The output keeps apple and cherry because their keys are absent from $discounted. Notice that banana and date are removed even though their values differ between the two arrays — array_diff_key() ignores values entirely:
Array
(
[apple] => 1.2
[cherry] => 3
)Comparing more than two arrays
You can pass any number of arrays. An entry is removed if its key appears in any of them:
<?php
$a = ["color" => "red", "size" => "M", "qty" => 5];
$b = ["color" => "blue"];
$c = ["qty" => 99];
print_r(array_diff_key($a, $b, $c));
?>Only size remains, because color is matched by $b and qty is matched by $c:
Array
(
[size] => M
)Keys are compared as strings
PHP casts all keys to strings before comparing them, so the integer key 0 and the string key "0" are treated as the same key. This matters when you mix numeric and string keys:
<?php
$nums = [0 => "a", 1 => "b", 2 => "c"];
$remove = ["0" => "x", "2" => "y"];
print_r(array_diff_key($nums, $remove));
?>Array
(
[1] => b
)Use Cases for array_diff_key
- Whitelisting/blacklisting fields. Strip keys you don't want from user input:
array_diff_key($input, array_flip(['password', 'token']))removes those two keys regardless of their values. - Finding missing configuration. Compare a set of defaults against a user-supplied config to see which keys were left out.
- Reconciling datasets. Identify records (keyed by ID) that exist in one array but not another.
- Cleaning request data before passing it to a function that rejects unexpected keys.
Tip: If you instead want to keep only the entries whose keys appear in another array, use
array_intersect_key()— it is the mirror image ofarray_diff_key().
array_diff_key vs. related functions
| Function | Compares by | Values considered? |
|---|---|---|
array_diff_key() | keys only | no |
array_diff() | values only | yes |
array_diff_assoc() | keys and values | yes |
array_diff_ukey() | keys via a callback | no |
If you need an additional value check on top of the key comparison, reach for array_diff_assoc(). To customize how keys are compared (for example, case-insensitive matching), use array_diff_ukey().
Conclusion
array_diff_key() is a focused, fast way to filter one array against the keys of others. Remember the two rules that trip people up: it compares keys only (values are ignored), and keys are cast to strings before comparison. For value-aware comparisons see array_diff_assoc(), and to test for a single key use array_key_exists().