PHP array_uintersect_assoc: Compute Array Intersection with Index Check Using a Callback
The article explains PHP's array_uintersect_assoc function, which returns the intersection of two arrays while preserving keys and using a user‑provided callback to compare values, and includes a detailed parameter description, return value, and a complete runnable example.
Function signature
array array_uintersect_assoc (array $array1, array $array2, callable $data_compare_func)Description
This function computes the intersection of $array1 and $array2 while preserving the keys of the first array. Comparison of values is performed by a user‑supplied callback function $data_compare_func, which must return an integer less than, equal to, or greater than zero when the first argument is considered less than, equal to, or greater than the second argument.
Parameters
array1 : The first array.
array2 : The second array.
data_compare_func : A callable that receives two values and returns an integer (<0, 0, >0) indicating the comparison result.
Return value
Returns an array containing all values from $array1 that are also present in $array2 (according to the comparison callback), preserving the original keys.
Example
<?php
$array1 = array(
"a" => "green",
"b" => "brown",
"c" => "blue",
"d" => "red"
);
$array2 = array(
"a" => "GREEN",
"b" => "brown",
"c" => "yellow",
"d" => "red"
);
print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));
?>Output
Array
(
[a] => green
)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
