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
Syntax
array_diff_key(array $array, array ...$arrays): array
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
arrays |
Arrays whose keys are excluded. |
Return value
The entries with keys only in the first array.
Examples
<?php
$input = ["name" => "Ada", "password" => "secret", "role" => "admin"];
print_r(array_diff_key($input, ["password" => true]));
Output
Array
(
[name] => Ada
[role] => admin
)<?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
| Version | Change |
|---|---|
| PHP 5.1 | Added |
Related
array_diff()· Return the values of the first array that are not in any of the others. Values are compared as strings.array_intersect_key()· Return the entries of the first array whose keys exist in all the other arrays. Great for whitelisting fields.- PHP Array Functions · tutorial
- PHP Arrays · tutorial