PHP 8 reference · Array Functions

PHP array_intersect_key() Function

Return the entries of the first array whose keys exist in all the other arrays. Great for whitelisting fields.

PHP 8 Since 5.1

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

Syntax

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

Parameters

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

Return value

The entries with matching keys.

Examples

Keep only allowed fieldsTry it yourself
<?php
$input = ["name" => "Ada", "email" => "[email protected]", "is_admin" => true];
$allowed = array_flip(["name", "email"]);
print_r(array_intersect_key($input, $allowed));

Output

Array
(
    [name] => Ada
    [email] => [email protected]
)
Order comes from the first arrayTry it yourself
<?php
print_r(array_intersect_key(["a" => 1, "b" => 2, "c" => 3], ["c" => 0, "a" => 0]));

Output

Array
(
    [a] => 1
    [c] => 3
)

Version history

VersionChange
PHP 5.1Added

Was this page helpful?