The array_intersect_ukey Function
The array_intersect_ukey
function takes several arrays and a callback function to compare their keys. The first parameter is the main array, followed by the arrays for comparison. The last parameter is the callback function, which should compare keys and return an integer (less than, equal to, or greater than zero).
Syntax
array_intersect_ukey(array $array1, array $array2, ..., callable $key_compare_func): array
Example
Compare the keys of two arrays using a custom function:
<?php
$array1 = ['a' => 1, 'b' => 2, 'c' => 3];
$array2 = ['a' => 4, 'c' => 5, 'd' => 6];
$res = array_intersect_ukey($array1, $array2, function($key1, $key2) {
return strcmp($key1, $key2);
});
print_r($res);
?>
Code execution result:
['a' => 1, 'c' => 3]
Example
Comparing keys of three arrays with case sensitivity:
<?php
$array1 = ['A' => 1, 'B' => 2, 'C' => 3];
$array2 = ['a' => 4, 'B' => 5, 'C' => 6];
$array3 = ['A' => 7, 'B' => 8, 'c' => 9];
$res = array_intersect_ukey($array1, $array2, $array3, function($key1, $key2) {
return strcmp($key1, $key2);
});
print_r($res);
?>
Code execution result:
['B' => 2]
See Also
-
the
array_intersect
function,
which computes the intersection of arrays by values -
the
array_intersect_key
function,
which computes the intersection of arrays by keys -
the
array_uintersect
function,
which computes the intersection of arrays with a callback function for value comparison