PHP array_diff_key() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_diff_key() compares the keys of the first array with the keys of other arrays. It returns the entries of the first array whose keys do not exist in any of the others.

You use array_diff_key() to remove or find entries by name, for example to strip unwanted fields from a form or to see which required settings are missing. The values are never compared, only the keys.

Syntax

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

PHP
$rest = array_diff_key($data, $blocked);
CallWhat it does
array_diff_key(['a' => 1, 'b' => 2], ['a' => 9])Returns ['b' => 2], because only the key 'a' exists in both.
array_diff_key([1, 2, 3], [5, 6])Returns [2 => 3], because the keys 0 and 1 exist in both.
array_diff_key($a, [])Returns all entries of $a, because no key is removed.

Only the keys are compared, and the values of the other arrays do not matter at all. The result keeps the original keys and values of the first array. A common trick is to use array_flip() on a list of names so you can remove those keys.

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

Parameters

ParameterDescription
arrayRequired. The array to compare from.
arraysOptional. One or more arrays whose keys are compared with the first array.

Return Value

Return ValueDescription
arrayThe entries of the first array whose keys are not in any of the other arrays.

PHP Examples

Example 1 — Removing Entries by Key

Keep only the entries whose keys are not present in the second array.

PHP
<?php
$a = ['a' => 1, 'b' => 2, 'c' => 3];
$b = ['a' => 99, 'c' => 100];

print_r(array_diff_key($a, $b));

How It Works

  1. The keys 'a' and 'c' exist in both arrays, so those entries are removed.
  2. The values 99 and 100 in $b are ignored.
  3. The key 'b' exists only in $a, so it is kept.
  4. The result is ['b' => 2].

Example 2 — Numeric Keys

Plain lists have the keys 0, 1, 2 and so on, and those keys are compared too.

PHP
<?php
$list = ['x', 'y', 'z'];
$other = [10, 20];

print_r(array_diff_key($list, $other));

How It Works

  1. $list has the keys 0, 1 and 2.
  2. $other has the keys 0 and 1.
  3. The keys 0 and 1 are in both arrays, so 'x' and 'y' are removed.
  4. Only key 2 is left, which holds 'z'.

Example 3 — Removing Sensitive Fields

Flip a list of blocked names so they become keys, then remove them from the data.

PHP
<?php
$user = [
    'name' => 'Asha',
    'email' => '[email protected]',
    'password' => 'secret',
    'token' => 'abc123',
];

$blocked = array_flip(['password', 'token']);

print_r(array_diff_key($user, $blocked));

How It Works

  1. array_flip() turns ['password', 'token'] into ['password' => 0, 'token' => 1].
  2. Now the names to block are keys, which is what array_diff_key() compares.
  3. The keys 'password' and 'token' exist in both arrays, so they are removed from $user.
  4. The safe fields 'name' and 'email' are returned.

Frequently Asked Questions

It returns the entries of the first array whose keys are missing from all the other arrays. Only keys are compared, not values.
array_diff() compares values, while array_diff_key() compares keys. The same array pair can give very different results with the two functions.
Put the key names in an array, use array_flip() to turn them into keys, and call array_diff_key($data, $flipped). The listed keys are removed from the data.
No. Only their keys matter, so you can use any value, such as true or 0, in the arrays you compare against.
No. It returns a new array and leaves the originals as they were.

Next: array_diff_uassoc()

array_diff_uassoc() works like array_diff_assoc(), but you provide your own callback funct.

array_diff_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