array_uintersect_assoc() compares values through your callback while also requiring the array keys to match using the normal key comparison.
$first = ['a' => 'RED', 'b' => 'green'];
$second = ['a' => 'red', 'b' => 'blue'];
$result = array_uintersect_assoc($first, $second, 'strcasecmp');
print_r($result);Keys are checked normally; only the value comparison uses your callback.
array_uintersect_assoc(array $array1, array $array2, callable $callback): array
function compare_values($a, $b) {
return strcmp($a, $b);
}
$array1 = ['a' => 'Apple', 'b' => 'Banana', 'c' => 'Cherry'];
$array2 = ['a' => 'apple', 'b' => 'Banana', 'c' => 'cherry'];
$result = array_uintersect_assoc($array1, $array2, 'compare_values');
print_r($result);
Output
Array
(
[b] => Banana
)
function num_compare($a, $b) {
return $a <=> $b;
}
$array1 = [0 => 10, 1 => 20, 2 => 30];
$array2 = [0 => 10, 2 => 30, 3 => 40];
$result = array_uintersect_assoc($array1, $array2, 'num_compare');
print_r($result);
Output
Array
(
[0] => 10
[2] => 30
)
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.