PHP 8 reference · Array Functions

PHP array_diff_key() Function

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

PHP 8 Since 5.1

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_diff_key(array $array, array ...$arrays): array

Parameters

ParameterDescription
array The input array.
arrays Arrays whose keys are excluded.

Return value

The entries with keys only in the first array.

Examples

Remove keysTry it yourself
<?php
$input = ["name" => "Ada", "password" => "secret", "role" => "admin"];
print_r(array_diff_key($input, ["password" => true]));

Output

Array
(
    [name] => Ada
    [role] => admin
)
Find missing fieldsTry it yourself
<?php
$required = ["name" => 1, "email" => 1];
$sent = ["name" => "Ada"];
echo "missing: ", implode(",", array_keys(array_diff_key($required, $sent))), "\n";

Output

missing: email

Version history

VersionChange
PHP 5.1Added

Was this page helpful?