PHP array_intersect_key() Function

The array_intersect_key() function compares the keys of two or more arrays and returns an array containing the keys present in all the arrays.

Key Features of array_intersect_key()

Syntax of array_intersect_key()

Syntax

array array_intersect_key(array $array1, array $array2, array ...$arrays);

Example Using array_intersect_key()

The following example demonstrates how array_intersect_key() is used:

Example


$array1 = ["a" => "PHP", "b" => "JavaScript", "c" => "Python"];
$array2 = ["a" => "Ruby", "b" => "Java", "d" => "C++"];

$result = array_intersect_key($array1, $array2);
print_r($result);

Output

Array ( [a] => PHP [b] => JavaScript )