PHP array_udiff() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_udiff() returns the values from the first array that are not found in the other arrays, using your own function to compare values.

The "u" stands for user-defined. Your callback decides when two values are equal, which lets you ignore case, compare by an ID, or use any custom rule. Keys are preserved.

Syntax

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

PHP
$diff = array_udiff($a, $b, fn($x, $y) => strcasecmp($x, $y));
CallWhat it does
array_udiff($a, $b, 'strcasecmp')Finds values in $a that are missing from $b, ignoring case.
array_udiff($a, $b, fn($x, $y) => $x <=> $y)Compares numbers with the spaceship operator.
array_udiff($a, $b, $c, $compare)Compares against several arrays at once.

The callback must return a negative number, zero or a positive number, like strcmp() or the spaceship operator <=>. Returning 0 means the two values are equal.

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

Parameters

ParameterDescription
arrayRequired. The first array. Values from this array are the ones that can appear in the result.
arraysRequired. One or more arrays to compare against.
value_compare_funcRequired. The callback used to compare two values. It must return an integer: less than, equal to, or greater than zero.

Return Value

Return ValueDescription
arrayAn array holding the values of the first array that are not in any other array. Original keys are kept.

PHP Examples

Example 1 — Case-Insensitive Difference

Find fruit names that are not in the second list, ignoring upper and lower case.

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

$diff = array_udiff($a, $b, fn($x, $y) => strcasecmp($x, $y));

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

How It Works

  1. The first array has three fruits and the second has two in lowercase.
  2. strcasecmp() compares two strings and ignores case.
  3. "Apple" matches "apple" and "Cherry" matches "cherry", so both are removed.
  4. Only "Banana" is left, and implode() prints it.

Example 2 — Keys Are Preserved

The result keeps the original keys, which can leave gaps.

PHP
<?php
$a = [1, 5, 10, 15];
$b = [5, 15];

$diff = array_udiff($a, $b, fn($x, $y) => $x <=> $y);

echo json_encode($diff), PHP_EOL;
echo json_encode(array_values($diff)), PHP_EOL;

How It Works

  1. The spaceship operator <=> returns -1, 0 or 1 when comparing two numbers.
  2. The values 5 and 15 exist in both arrays, so they are removed.
  3. The remaining values 1 and 10 keep their old keys 0 and 2, so json_encode() shows an object.
  4. array_values() renumbers the keys, giving a clean list.

Example 3 — Compare Records by ID

Compare arrays of data by looking at only one field.

PHP
<?php
$all = [
    ["id" => 1, "name" => "Ann"],
    ["id" => 2, "name" => "Ben"],
    ["id" => 3, "name" => "Cara"],
];
$banned = [
    ["id" => 2, "name" => "Ben"],
];

$allowed = array_udiff($all, $banned, fn($x, $y) => $x["id"] <=> $y["id"]);

foreach ($allowed as $user) {
    echo $user["name"], PHP_EOL;
}

How It Works

  1. Each user is a small array with an id and a name.
  2. The callback compares only the id values of two users.
  3. The user with id 2 appears in the banned list, so it is removed.
  4. The loop prints the names of the remaining users.

Frequently Asked Questions

It returns the values of the first array that are not present in the other arrays, using a callback you write to compare values.
array_diff() uses a built-in string comparison. array_udiff() lets you supply your own compare function.
An integer: negative if the first value is smaller, zero if they are equal, and positive if the first is larger. The <=> operator and strcmp() both do this.
No. Only values are compared. Use array_udiff_assoc() if the keys must match too.
It must be the last argument, after all the arrays.

Next: array_udiff_assoc()

array_udiff_assoc() returns the items of the first array that are not in the other arrays,.

array_udiff_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