Home >>PHP Array Functions >PHP array_intersect_key() Function

PHP array_intersect_key() Function

PHP array_intersect_key() Function

PHP array_intersect_key() Function is used to calculate the intersection of two or more given arrays. It uses the keys for the comparison and returns the matching key elements as output. It prints only those elements of the first array whose keys matches with the elements of all other arrays. It can accept any number of arrays greater than or equal to two.

Syntax:

array_intersect_key($array1, $array2, $array3, ...);

Parameter Values

Parameter Description
array1 This is a required parameter. This parameter defines the array that the others will be compared with.
array2 This is a required parameter. This parameter defines an array to be compared with the first array.
array3,... This is an optional parameter. This parameter defines more array to be compared with the first array.

Here is an example of array_intersect_key() function in PHP:

<html>
<body>
<pre>
<?php
$x=array("a"=>"Abhi","b"=>"Jerry","c"=>"Noida","d"=>"Python");
$y=array("a"=>"Abhi","c"=>"Noida","d"=>"PHP");
$result=array_intersect_key($x,$y);
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => Abhi
    [c] => Noida
    [d] => Python
)

Example 2:

<html>
<body>
<pre>
<?php
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5");
$y=array("a"=>"1","b"=>"12","c"=>"33","d"=>"5","e"=>6);
$result=array_intersect_key($x,$y);
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
)

No Sidebar ads