PHP array_uintersect() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_uintersect() returns the values of the first array that also exist in all the other arrays, using your own function to compare values.

It is the opposite of array_udiff(). Your callback decides when two values are equal, so you can ignore case, compare absolute values, or match records by an ID.

Syntax

Call array_uintersect() with the values you need. Start with this line.

PHP
$common = array_uintersect($a, $b, 'strcasecmp');
CallWhat it does
array_uintersect($a, $b, 'strcasecmp')Finds values in both arrays, ignoring case.
array_uintersect($a, $b, fn($x, $y) => $x <=> $y)Compares numbers with the spaceship operator.
array_uintersect($a, $b, $c, $compare)Keeps only values found in all three arrays.

The result always contains the values as they appear in the first array. Keys are preserved.

Library declarations
PHP
array_uintersect(array $array, array ...$arrays, callable $value_compare_func): array

Parameters

ParameterDescription
arrayRequired. The first array. Its values and keys are used in the result.
arraysRequired. One or more arrays to compare with.
value_compare_funcRequired. Callback that compares two values and returns an integer (negative, zero or positive).

Return Value

Return ValueDescription
arrayAn array with the values of the first array that appear in every other array. Keys are preserved.

PHP Examples

Example 1 — Common Values, Ignoring Case

Find fruit names that appear in both lists, whatever the capitalization.

PHP
<?php
$a = ["Apple", "Banana", "Cherry"];
$b = ["banana", "cherry", "date"];

$common = array_uintersect($a, $b, fn($x, $y) => strcasecmp($x, $y));

echo implode(", ", $common), PHP_EOL;

How It Works

  1. The two lists use different capitalization.
  2. strcasecmp() treats "Banana" and "banana" as equal.
  3. "Apple" is not in the second list, and "date" is not in the first.
  4. The result uses the spelling from the first array: Banana and Cherry.

Example 2 — Custom Rule: Absolute Values

Your callback can define any idea of equality, such as ignoring a minus sign.

PHP
<?php
$a = [-3, 1, 2];
$b = [3, -1];

$common = array_uintersect($a, $b, fn($x, $y) => abs($x) <=> abs($y));

echo json_encode($common), PHP_EOL;

How It Works

  1. The callback compares the absolute values of two numbers.
  2. -3 matches 3 because both have an absolute value of 3.
  3. 1 matches -1 for the same reason, but 2 has no partner.
  4. The result keeps the original values -3 and 1 from the first array.

Example 3 — Intersect Three Arrays

A value must be in every array to survive.

PHP
<?php
$a = ["red", "green", "blue", "pink"];
$b = ["RED", "GREEN", "BLUE"];
$c = ["Green", "Blue", "Black"];

$common = array_uintersect($a, $b, $c, fn($x, $y) => strcasecmp($x, $y));

echo implode(", ", $common), PHP_EOL;

How It Works

  1. Three arrays are passed, followed by the compare callback.
  2. "red" is missing from the third array, so it is removed.
  3. "pink" is missing from the second and third arrays, so it is removed too.
  4. "green" and "blue" appear in all three arrays and remain.

Frequently Asked Questions

It returns the values from the first array that are also in every other array, comparing values with a callback you provide.
array_intersect() uses a built-in comparison. array_uintersect() lets you define what "equal" means.
No. It only compares values. Use array_uintersect_assoc() to compare keys as well.
The values (and keys) come from the first array.
It must be the last argument, after all the arrays.

Next: array_uintersect_assoc()

array_uintersect_assoc() returns the entries of the first array that exist in all other ar.

array_uintersect_assoc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful