Definitions and usage
The Array_intersect_key () function uses the key name comparison to compute the intersection of an array.
Array_intersect_key () returns an array that contains the values of all the key names that appear in the array being compared and appear in all other parameter arrays at the same time.
Note: Only key names are used for comparisons.
Grammar
Array_intersect_key (Array1,array2,array3 ...)
Parameters
Describe
Array1 required. The first array to compare with the other arrays.
Array2 required. The array to compare with the first array.
Array3 Optional. The array to compare with the first array. can have multiple.
Example 1
<?php $a 1=array (0=> "Cat",1=> "Dog",2=> "horse"); $a 2=array (2=> "Bird",0=> "Cat",4=> "Fish"); Print_r (Array_intersect_key ($a 1, $a 2));?>
Output:
Array ([0] => Cat [2] => horse)
Example 2
<?php $a 1=array (0=> "Cat",1=> "Dog",2=> "horse"); $a 2=array (2=> "Bird",3=> "Rat",4=> "Fish"); $a 3=array (2=> "Dog",6=> "Cow",7=> "Bird"); Print_r (Array_intersect_key ($a 1, $a 2, $a 3));?>
Output:
Array ([2] => horse)