array_diff_key
Returns entries of the first array whose keys are not present in any of the other arrays.
array_diff_key(array $array, array ...$arrays): array- Added in PHP 5.1.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array to compare from. |
$arraysoptional | array | One or more arrays whose keys are compared against (variadic). |
Return value
arrayAn array of all entries from $array whose keys are absent from every other array. Keys and values are preserved.
Changelog
- PHP 8.0.0Can now be called with a single argument; previously at least two arrays were required.
array_diff_key() compares keys only and returns the entries of the first array whose keys do not appear in any of the others. Values are never inspected, which makes it fast and completely type-safe for the data itself.
When to reach for it
It is the cleanest way to strip fields from an array. Pass user input plus a blocklist of keys such as id, is_admin and csrf, and what comes back is safe to hand to a mass-assignment routine. It also answers the reverse question: array_diff_key($required, $provided) lists the config keys someone forgot to set, and the values in the required array can carry the error messages.
Because only keys matter, the comparison arrays can hold anything — true, null, an error string. Many codebases use array_flip() on a plain list of key names to build the second argument.
What trips people up
Keys are compared as strings, but remember PHP normalises numeric string keys to integers on the way in, so '10' and 10 are the same key and will match. Keys that differ only in case or in surrounding whitespace do not match — use array_diff_ukey() with strcasecmp if you need looser matching.
Example
When to use it
- Strip protected fields such as id and is_admin out of a request payload before mass assignment
- List which required configuration keys are missing from a loaded .env array
- Keep only the cache entries whose keys were not just invalidated
Frequently asked questions
What is the difference between array_diff_key and array_diff?
array_diff_key compares only the keys and never looks at the values, while array_diff compares only the values and ignores the keys. Use array_diff_assoc when both must match.
How do I remove a list of keys from an array?
Flip the list into keys first: array_diff_key($array, array_flip(['id', 'token'])). Passing the plain list would compare against its numeric indexes instead of the names.
Does array_diff_key look at the values of the other arrays?
No. Only the keys of the second and later arrays are used, so their values can be null, true, or anything convenient — they never affect the result.
More examples
Finding missing configuration keys
Because values are ignored during comparison, the required array can double as a message table. The output names app_key and prints the human-readable label stored alongside it.
<?php
$required = ['db_host' => 'database host', 'db_name' => 'database name', 'app_key' => 'app key'];
$provided = ['db_host' => 'localhost', 'db_name' => 'shop'];
foreach (array_diff_key($required, $provided) as $key => $label) {
echo "missing $key ($label)\n";
}Excluding several key sets at once
Both exclusion sets are applied in one call, leaving name and email. This is the usual shape for building a safe API response out of a database row.
<?php
$row = ['id' => 1, 'name' => 'Ada', 'password' => 'x', 'token' => 'y', 'email' => 'a@x'];
$internal = ['id' => null];
$secrets = ['password' => null, 'token' => null];
print_r(array_diff_key($row, $internal, $secrets));Numeric keys are normalised
The string '10' was stored as integer 10 and the float 10.9 was truncated to 10 as well, so both collapse into one key that matches $b and is removed. Only 'x' survives — a reminder that key comparison happens after PHP's own key casting.
<?php
$a = ['10' => 'ten', 10.9 => 'float', 'x' => 1];
$b = [10 => 'other'];
var_dump(array_diff_key($a, $b));
Discussion