array_intersect_key

Keeps the entries of the first array whose keys also exist in every other array.

Signature
array_intersect_key(array $array, array ...$arrays): array
  • Added in PHP 5.1.0

Parameters

ParameterTypeDescription
$arrayarrayThe array with the master keys to check.
$arraysoptionalarrayOne or more arrays whose keys are compared against.

Return value

arrayAn array containing the entries of $array whose keys are present in all of the other arrays.

Errors

Passing a non-array argument throws a TypeError.

Changelog

  • PHP 8.0.0Can now be called with a single argument; previously at least two arrays were required.

array_intersect_key() compares keys and ignores values entirely. The values in the result always come from the first array, so the other arrays act purely as a key filter — their values can be anything, including null.

The whitelist idiom

This is the cleanest way in core PHP to reduce an untrusted payload to a known set of fields:

$safe = array_intersect_key($input, array_flip(['name', 'email']));

array_flip() turns your list of allowed names into keys, and anything the client sent that you did not name is dropped. Unlike a hand-written loop, a key that is missing from $input simply does not appear — it is not created as null.

Key comparison rules

Keys compare by their string form, but because PHP already normalises array keys on insertion, the integer key 3 and the string key "3" are the same key before the function ever sees them. Numeric-looking string keys such as "03" stay strings and will not match the integer 3.

The complement

array_diff_key() does the opposite — it removes named keys instead of keeping them, which is a blacklist rather than a whitelist.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Reduce a request body to the columns a model is allowed to mass-assign.
  • Extract a subset of a large config array by naming the keys you need.
  • Keep only the cache entries whose ids you just fetched from the database.

Frequently asked questions

How do I keep only certain keys from an array in PHP?

Use array_intersect_key($array, array_flip($allowedKeys)). array_flip turns your list of names into keys so they can be compared.

Does array_intersect_key compare values at all?

No. Only keys are compared, and the values in the result always come from the first array. The comparison arrays can hold nulls or dummy values.

What is the difference between array_intersect_key and array_diff_key?

array_intersect_key keeps the keys that are present in every array, while array_diff_key keeps the keys of the first array that are absent from the others. One is a whitelist, the other a blacklist.

More examples

Whitelist versus blacklist

array_intersect_key names what to keep, array_diff_key names what to drop. Both hide password_hash here, but the whitelist stays safe if a new sensitive column is added later.

Example · php
<?php
$row = ['id' => 7, 'name' => 'Ada', 'password_hash' => 'secret', 'email' => '[email protected]'];

$public = array_intersect_key($row, array_flip(['id', 'name', 'email']));
$hidden = array_diff_key($row, array_flip(['password_hash']));

echo json_encode($public), "\n";
echo json_encode($hidden), "\n";

Intersecting the keys of three arrays

A key must exist in every argument, so only host and port survive; user is missing from $defaults and debug is missing from $schema. The second line shows the default keys the schema does not define.

Example · php
<?php
$defaults = ['host' => 'localhost', 'port' => 3306, 'debug' => false];
$schema   = ['host' => 1, 'port' => 1, 'user' => 1];
$provided = ['host' => 1, 'debug' => 1, 'port' => 1];

print_r(array_intersect_key($defaults, $schema, $provided));
print_r(array_keys(array_diff_key($defaults, $schema)));

Discussion

  • Be the first to comment on this lesson.