The array_uintersect_assoc Function
The array_uintersect_assoc
function returns an array containing all the elements of the first array that are present in all other arrays. Comparison of keys and values is performed using a user-defined callback function.
Syntax
array_uintersect_assoc(array $array1, array $array2, ..., callable $value_compare_func): array
Example
Comparing arrays with a custom function:
<?php
function compare($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
$array1 = ["a" => "green", "b" => "brown", "c" => "blue"];
$array2 = ["a" => "GREEN", "B" => "brown", "c" => "blue"];
print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));
?>
Code execution result:
Array
(
[a] => green
[c] => blue
)
Example
Comparison with numeric values:
<?php
function numCompare($a, $b) {
return $a <=> $b;
}
$array1 = [10 => "apple", 20 => "banana", 30 => "cherry"];
$array2 = [10 => 10, 20 => "banana", 40 => "cherry"];
print_r(array_uintersect_assoc($array1, $array2, "numCompare"));
?>
Code execution result:
Array
(
[20] => banana
)
See Also
-
the
array_intersect_assoc
function,
which computes the intersection of arrays with index check -
the
array_uintersect
function,
which computes the intersection of arrays with a callback function (without index check)