PHP array_intersect_key() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_intersect_key() returns the items of the first array whose keys also exist in every other array.

Only the keys are compared. The values in the other arrays are ignored, and the original keys are kept.

Syntax

Call array_intersect_key() with the values you need. Start with this line.

PHP
$common = array_intersect_key($first, $second);
CallWhat it does
array_intersect_key($a, $b)Keeps items from $a whose keys are also in $b.
array_intersect_key($a, $b, $c)Keeps only keys found in all three arrays.
array_intersect_key($a, array_flip($allowed))Keeps only the keys listed in $allowed.

The returned values always come from the first array. Think of the other arrays as a list of allowed keys.

Library declarations
PHP
array_intersect_key(array $array, array ...$arrays): array

Parameters

ParameterDescription
arrayRequired. The array to check. Its values are returned.
arraysOptional. One or more arrays whose keys are compared with the first array.

Return Value

Return ValueDescription
arrayA new array with the items of the first array whose keys exist in all other arrays.

PHP Examples

Example 1 — Keep Matching Keys

Compare two arrays and keep only the keys that appear in both.

PHP
<?php
$a = ["red" => 1, "green" => 2, "blue" => 3];
$b = ["red" => 10, "blue" => 30, "pink" => 40];

$result = array_intersect_key($a, $b);

echo json_encode($result), PHP_EOL;
echo count($result), PHP_EOL;

How It Works

  1. Create two arrays. They share the keys red and blue.
  2. Call array_intersect_key(). PHP checks each key of $a against $b.
  3. Read the result. The values 1 and 3 come from $a, not from $b.
  4. Count the items. Two keys matched, so count() prints 2.

Example 2 — Numeric Keys, No Match and Many Arrays

Keys are kept as they are, an empty result is possible, and you can pass more than two arrays.

PHP
<?php
$list = [10, 20, 30, 40];
$wanted = [1 => "x", 3 => "y"];
echo json_encode(array_intersect_key($list, $wanted)), PHP_EOL;

echo json_encode(array_intersect_key(["a" => 1], ["b" => 2])), PHP_EOL;

$c = ["a" => 1, "b" => 2, "c" => 3];
echo json_encode(array_intersect_key($c, ["a" => 0, "b" => 0], ["b" => 0, "c" => 0])), PHP_EOL;

How It Works

  1. Use numeric keys. Keys 1 and 3 match, so the items 20 and 40 stay.
  2. Notice the keys are kept. The result is not renumbered, so JSON shows it as an object.
  3. Try arrays with no shared key. The result is an empty array [].
  4. Pass three arrays. Only key b exists in all of them, so only "b" => 2 remains.

Example 3 — Whitelist Form Fields

A common use is to drop unexpected fields from user input.

PHP
<?php
$input = ["name" => "Ana", "email" => "[email protected]", "admin" => "1"];
$allowed = array_flip(["name", "email"]);

$clean = array_intersect_key($input, $allowed);

foreach ($clean as $key => $value) {
    echo $key . ": " . $value . PHP_EOL;
}

How It Works

  1. Start with raw input. It contains a risky admin field.
  2. Flip the allowed list. array_flip() turns the names into keys.
  3. Intersect the keys. Only name and email survive.
  4. Loop over the clean array. The admin value is gone.

Frequently Asked Questions

It returns the items of the first array whose keys are also present in all the other arrays. Only keys are compared, never values.
array_intersect_key(array $array, array ...$arrays): array. The first array supplies the values, and the extra arrays supply the keys to match.
array_intersect() compares values, while array_intersect_key() compares keys. The second one is better when you care about field names.
Yes. The result keeps the original keys of the first array, so numeric keys are not renumbered.
Use array_intersect_key($array, array_flip($allowedKeys)). It is a short way to whitelist keys.

Next: array_intersect_uassoc()

array_intersect_uassoc() returns the items of the first array that exist in all other arra.

array_intersect_uassoc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful