array_uintersect
Returns the values present in every array, compared with a callback you supply.
array_uintersect(array $array, array ...$arrays, callable $value_compare_func): array- Added in PHP 5.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The first array, whose values and keys appear in the result. |
$arrays | array | One or more arrays to intersect with. Variadic. |
$value_compare_func | callable | Comparison function with the signature callback(mixed $a, mixed $b): int, returning less than, equal to, or greater than zero. Always the last argument. |
Return value
arrayAn array of the values from $array that are present in all of the other arrays, with the original keys preserved.
Errors
Throws a TypeError if fewer than three arguments are supplied, if any array argument is not an array, or if the last argument is not callable.
The callback comes last, after all the arrays, and must return an integer ordering. A boolean callback goes wrong in the opposite direction from what people expect: false becomes 0, which the function reads as "equal", so values your callback considered different are treated as matches and the entire first array comes back. A suspiciously complete result — every element survived the intersection — is the usual symptom of a boolean comparator, and PHP 8.3 and later also raise a deprecation notice for it.
Example
When to use it
- Find which cached objects also exist in a fresh API response, matching on id.
- Intersect two lists of tags or emails ignoring letter case.
- Match rows from two datasets on a normalised key such as a trimmed, lowercased name.
Frequently asked questions
What is the difference between array_intersect and array_uintersect?
array_intersect compares values as strings, while array_uintersect calls your callback. Use array_uintersect for objects, floats, or case-insensitive matching.
Why does array_uintersect return an empty array?
Not because of a boolean callback — that produces the opposite symptom, returning every element of the first array, since false becomes 0 and 0 means "equal". An empty result usually means the comparator is not a consistent total ordering: array_uintersect sorts each array with your callback before matching, so a fuzzy "is this a match" callback, or one that compares the wrong field, makes genuine matches be missed. Return a real integer ordering from the spaceship operator or strcmp().
Which keys does array_uintersect return?
The keys and values of the matching elements from the first array. Elements from the later arrays are only used for comparison.
More examples
Intersecting objects by id
Prints key 0 -> user 1 and key 2 -> user 3. The objects returned are the ones from the first array, with their original keys, even though equal objects existed in the second.
<?php
class User { public function __construct(public int $id) {} }
$members = [new User(1), new User(2), new User(3)];
$active = [new User(3), new User(1)];
$both = array_uintersect($members, $active, fn($a, $b) => $a->id <=> $b->id);
foreach ($both as $k => $u) { echo "key $k -> user {$u->id}\n"; }Intersecting three arrays
A value must be present in every array to survive, so red drops out. green and blue remain at their original keys 1 and 2, spelled as they were in the first array.
<?php
$a = ['red', 'green', 'blue'];
$b = ['GREEN', 'blue', 'yellow'];
$c = ['Blue', 'Green'];
print_r(array_uintersect($a, $b, $c, 'strcasecmp'));Duplicates on the left are kept
Both copies of 'a' survive, at keys 0 and 2, because the intersection is per element rather than per distinct value. array_unique plus array_values collapses them into a clean single-element list.
<?php
$left = ['a', 'b', 'a', 'c'];
$right = ['a'];
$hits = array_uintersect($left, $right, 'strcmp');
print_r($hits);
print_r(array_values(array_unique($hits)));
Discussion