Understanding the PHP Function: array_intersect_key()
The PHP function array_intersect_key() is a useful tool for comparing arrays and returning only the values with matching keys. In this article, we will dive
The PHP function array_intersect_key() compares two or more arrays by their keys and returns a new array containing the entries from the first array whose keys exist in every other array. Crucially, it ignores the values entirely — only the keys decide what is kept, and the values returned are always those of the first array. This page covers how the function works, when to reach for it, and the gotchas that trip people up.
Purpose
array_intersect_key() answers the question: "Which entries of this array also appear — by key — in all of these other arrays?" A common real-world use is whitelisting form input or configuration: you keep only the keys you explicitly allow and drop everything else in one call.
It belongs to the same family as array_intersect(), which compares by value, and array_diff_key(), which returns the keys that are missing from the other arrays.
Syntax
array_intersect_key(array $array, array ...$arrays): array| Parameter | Description |
|---|---|
$array | The array to keep entries from. Its keys are checked against every other array, and its values are the ones returned. |
$arrays | One or more arrays whose keys are compared against $array. Their values are never used. |
An entry survives only if its key is present in all the supplied arrays. The original key/value pairs from the first array are preserved in the result.
Examples
Comparing two arrays
The keys shared by both arrays are a and b, so those entries are kept. Note that the values differ for key b (brown vs yellow) — that doesn't matter, only the key has to match, and the value comes from $array1. The bare "red" element in $array1 gets the implicit numeric key 0, which is absent from $array2, so it is dropped:
Array
(
[a] => green
[b] => brown
)Comparing three arrays
When you pass more than two arrays, a key must appear in every array to survive.
Only key a is present in all three arrays. Key b is missing from $array3 and key c is missing from $array2, so both are dropped:
Array
(
[a] => green
)Whitelisting an array by its keys
The most practical use of array_intersect_key() is filtering an associative array down to an approved set of keys — for example, accepting only known fields from user input:
<?php
$input = array(
"name" => "Ann",
"email" => "[email protected]",
"is_admin" => true, // attacker-supplied field we must ignore
);
$allowed = array("name" => "", "email" => "");
$safe = array_intersect_key($input, $allowed);
print_r($safe);
?>The values of $allowed are irrelevant — it acts purely as a list of permitted keys. The dangerous is_admin field is stripped out:
Array
(
[name] => Ann
[email] => [email protected]
)Things to keep in mind
- Only keys are compared, never values. Two entries with the same key but different values still match. The returned values always come from the first array.
- The order of the arrays doesn't change which keys survive, but the order of entries in the result follows the first array.
- Key comparison is case-sensitive for string keys:
"A"and"a"are different keys. - Numeric keys are compared as integers. The string key
"1"and the integer key1are treated as the same key, because PHP normalizes numeric-string array keys to integers. - Passing a single array simply returns that array unchanged; passing a non-array argument raises a
TypeError.
Related functions
array_intersect()— intersect by value instead of key.array_intersect_assoc()— match both key and value.array_diff_key()— the inverse: keep entries whose keys are not in the other arrays.array_key_exists()— test for a single key.- Working with PHP arrays — the broader array reference.