PHP array_intersect_ukey() Function

What is array_intersect_ukey() Function?


The "array_intersect_ukey()" function is used to compute the intersection of array elements using a callback function on the keys for comparison.



In the above syntax values and keys of "array1" is compared with other arrays using keys and again checked in the function for keys. The values and keys are returned in a array

The difference between array_intersect_ukey() and array_intersect_key() is that array_intersect_ukey() function uses a user defined function to find the intersecting array elements using keys.

Example :

function func1($k1, $k2)
if ($k1 == $k2)
return 0;
else if ($k1 > $k2)
return 1;
return -1;
$a = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$b = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'
=> 8);
var_dump(array_intersect_ukey($a, $b, 'func1'));
Result :

array(2) {["blue"]=> int(1) ["green"]=> int(3)}

In the above example the two arrays "$a","$b" are compared for array elements keys automatically, then compared for keys in the function. It returns intersection result of both the values and keys.

PHP Topics

Ask Questions

Ask Question