Comprehensive Guide to PHP's array_uintersect() Function
Learn how PHP's array_uintersect() compares arrays using a custom callback to return common values. Covers syntax, examples, and use cases.
The array_uintersect() function compares the values of two or more arrays and returns the values from the first array that are also present in every other array. What makes it different from array_intersect() is the u prefix: you supply the comparison logic as a callback, so you control exactly when two values count as equal.
This page covers the syntax, how the comparison callback works, runnable examples, and when to reach for this function instead of its simpler relatives.
Syntax
array_uintersect(
array $array1,
array $array2,
array ...$arrays,
callable $value_compare_func
): array| Parameter | Description |
|---|---|
$array1 | The base array. Its keys and values are preserved in the result. |
$array2, ...$arrays | One or more arrays to compare against. A value survives only if it appears in all of them. |
$value_compare_func | A callback that decides when two values are equal. It is always the last argument. |
The function returns a new array of the values from $array1 that are found in every other array, with the original keys from $array1 kept intact.
How the comparison callback works
The callback receives two values and must return an integer:
- a value less than 0 if the first argument is "less than" the second,
- 0 if the two values are considered equal,
- a value greater than 0 if the first is "greater than" the second.
Only a return value of 0 counts as a match. This is the same three-way contract used by sorting functions like usort(), which is why the built-in strcmp() makes a convenient callback for strings.
array_uintersect() does no type juggling of its own — every equality decision goes through your callback. That means you can make matching as loose or as strict as you need (case-insensitive, type-strict, object-aware, and so on).
A simple string callback looks like this:
function compare_strings($string1, $string2) {
return strcmp($string1, $string2);
}Basic example
Here we intersect three arrays of fruit names using the callback above:
<?php
function compare_strings($string1, $string2) {
return strcmp($string1, $string2);
}
$array1 = array("apple", "orange", "banana");
$array2 = array("orange", "banana", "kiwi");
$array3 = array("banana", "kiwi", "grape");
$result = array_uintersect($array1, $array2, $array3, "compare_strings");
print_r($result);
?>Output:
Array
(
[2] => banana
)Only "banana" appears in all three arrays. Notice the key is 2 — that is its original index in $array1, preserved in the result. "orange" is dropped because it is missing from $array3.
Case-insensitive matching
Because the callback owns all the comparison logic, swapping in strcasecmp() makes the intersection ignore letter case:
<?php
$a = ["Red", "GREEN", "Blue"];
$b = ["red", "green", "yellow"];
$result = array_uintersect($a, $b, "strcasecmp");
print_r($result);
?>Output:
Array
(
[0] => Red
[1] => GREEN
)"Red" and "GREEN" are kept because strcasecmp() treats them as equal to "red" and "green". The values returned are the ones from the first array, so the original casing is retained.
When to use array_uintersect()
Reach for this function when:
- You need the intersection of array values (not keys), and
- A plain equality check is not enough — the values are objects, mixed types, or need fuzzy/normalized matching.
If you only need to compare values with PHP's default loose comparison, array_intersect() is simpler. If keys matter too, see array_uintersect_assoc(), and for difference (rather than intersection) with a callback, see array_udiff().
Key benefits:
- Find common elements across many arrays in one call, with no manual loops.
- Original keys from the first array are preserved for easier data mapping.
- Custom comparison logic handles complex data types and specific matching rules.
Conclusion
array_uintersect() is the right tool when you need the common values of several arrays but PHP's built-in comparison does not match the way you define equality. Define a callback that returns 0 for equal values, pass it as the last argument, and the function returns the matching values from the first array with their keys intact.