PHP: array_intersect_ ukey() function

PHP: Computes the intersection of arrays using a callback function

The array_intersect_ukey() function is used to compare two or more arrays, against keys, using a user-supplied function.

The function returns an array containing all the values of the first array which have matching keys that are present in all the arguments.


(PHP 4 and above)


array_intersect_ukey(array_name1, array_name2, array_name3.......user-defined-function)


Name Description Required /
array_name1 Initial array for comparison with other arrays. Required Array
array_name2 The first array to compare with keys of array_name1. Required Array
array_name3 The second array to compare with keys of array_name1. Optional Array
user-defined-function User supplied function to do the comparison. Required Function

Return value:

The values of array_name1 whose keys exist in all the arguments.

Value Type: Array

Example :

function user_function($ukey1, $ukey2)
if ($ukey1 == $ukey2)
return 0;
else if ($ukey1 > $ukey2)
return 1;
return -1;
$array1 = array("Orange" => 1, "Apple" => 2, "Banana" => 3, "Chery" => 4);
$array2 = array("Orange" => 5, "Banana" => 6, "Mango" => 7, 'Guava'=> 8);
var_dump(array_intersect_ukey($array1, $array2,"user_function"));


array(2){ ["Orange"]=> int(1) ["Banana"]=> int(3)}

Pictorial Presentation:

php function reference: array_intersect_ ukey() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous:array_intersect_ uassoc
Next: array_intersect

PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);