PHP array_intersect_key() Function

What is array_intersect_key() Function?

Explanation

The "array_intersect_key()" function is used to compute the intersection of array elements, only if the keys are present in all other arrays.

Syntax:


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

In the above syntax keys of "array1" is compared with other array elements, to return an array with values of keys that are present in all other arrays.

Example :


<?php
$a = array (0=>'Blue',1=>'Green', 2=>'Orange', 3=>'Pink');
$b = array (4=>'Red', 2=>'Green', 3=>'Orange');
print_r(array_intersect_key($a,$b));
?>
Result :

Array ( [2] => Orange [3] => Pink )

In the above example the two arrays "$a","$b" when compared for keys, returns "Orange","Pink", as the keys 3,2 are present in both the arrays.

PHP Topics


Ask Questions

Ask Question