The array_diff_ukey Function
The array_diff_ukey function compares the keys of several arrays and returns elements from the first array whose keys are absent in the other arrays. A user-defined callback function is used to compare the keys. The first parameter is the main array, the subsequent parameters are arrays for comparison, and the last parameter is the callback function.
Syntax
array_diff_ukey(array $array1, array $array2 [, array $... ], callable $key_compare_func): array
Example
Let's compare two arrays by keys using the strcasecmp callback function (case-insensitive comparison):
<?php
$array1 = ['A' => 1, 'b' => 2, 'C' => 3];
$array2 = ['a' => 4, 'B' => 5];
$res = array_diff_ukey($array1, $array2, 'strcasecmp');
print_r($res);
?>
Code execution result:
['C' => 3]
Example
Let's compare three arrays with a custom callback function:
<?php
function keyCompare($key1, $key2) {
return $key1 <=> $key2;
}
$array1 = [1 => 'a', 2 => 'b', 3 => 'c'];
$array2 = [1 => 'd', 4 => 'e'];
$array3 = [2 => 'f'];
$res = array_diff_ukey($array1, $array2, $array3, 'keyCompare');
print_r($res);
?>
Code execution result:
[3 => 'c']
See Also
-
the
array_difffunction,
which compares arrays by values -
the
array_diff_keyfunction,
which compares arrays by keys -
the
array_udifffunction,
which compares arrays via a callback function