PHP array_intersect_key() function

Last Updated: February 12, 2022

array_intersect_key : Computes the intersection of arrays using keys for comparison

Syntax

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

The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2

Parameters

Parameter Description
array Required. array with values to check intersected values
arrays Required. arrays to be compared with the first array

Return Value

Return an array containing elements that are also appearing in other arrays

Example

<?php
$array1 = array("1" => "one", "2" => "two", "3" => "three", "four");
$array2 = array("1" => "one", "3" => "three", "four", "five");
$result_array = array_intersect_key($array1, $array2);
print_r($result_array);
?>

Output

Array ( [1] => one [3] => three [4] => four ) 
Test