PHP: array_diff_ukey() function

PHP: Compares the keys from an array against the keys from another

The array_diff_ukey() function is used to compare two or more arrays using an additional user supplied function on the keys for comparison.The function compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.


(PHP 4 and above)


array_diff_ukey(array1, array2, array3,.....user-defined-function)


Name Description Required /
array1 The specified array which will be compared with other arrays. Required Array
array2 Compared with the first array. Required Array
array3 Compared with the first array. Optional Array
user-defined-function The specified callback function. The function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. Required -

Return value:

An array containing all the entries from array1 that are not present in any of the other arrays i.e. array2, array3.

Value Type: Array


function abc($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('Banana' => 5, 'Apple' => 6, 'Mango' => 7, 'Guava'=> 8);
var_dump(array_diff_ukey($array1, $array2, 'abc'));


array(2) {["Orange"]=> int(1)["Chery"]=> int(4)} 

Pictorial Presentation:

php function reference: array_diff_ukey() function

View the example in the browser

Practice here online:

See also

PHP Function Reference

Next: array_diff

PHP: Tips of the Day

PHP: Extract numbers from a string

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