PHP array_udiff function tutorial shows how to compute array differences in PHP. Learn array_udiff with practical examples.
last modified March 13, 2025
The PHP array_udiff function computes the difference of arrays using a callback function for data comparison. It’s useful for custom comparisons.
The array_udiff function compares array values using a callback. It returns values from the first array not present in other arrays.
Syntax: array_udiff(array $array1, array $array2, …, callable $value_compare_func): array. The callback must return an integer less than, equal to, or greater than zero.
This shows a simple comparison of arrays containing numbers using a callback.
basic_array_udiff.php
<?php
declare(strict_types=1);
function compareNumbers(int $a, int $b): int { return $a <=> $b; }
$array1 = [1, 2, 3, 4, 5]; $array2 = [3, 4, 5, 6, 7];
$result = array_udiff($array1, $array2, ‘compareNumbers’);
print_r($result);
This finds elements in $array1 not in $array2. The callback uses the spaceship operator for comparison. The result contains values 1 and 2.
Compare arrays of objects based on specific object properties.
object_comparison.php
<?php
declare(strict_types=1);
class Product { public function __construct( public string $name, public float $price ) {} }
function compareProducts(Product $a, Product $b): int { return $a->price <=> $b->price; }
$products1 = [ new Product(“Laptop”, 999.99), new Product(“Phone”, 699.99), new Product(“Tablet”, 399.99) ];
$products2 = [ new Product(“Monitor”, 299.99), new Product(“Keyboard”, 99.99), new Product(“Phone”, 699.99) ];
$result = array_udiff($products1, $products2, ‘compareProducts’);
foreach ($result as $product) { echo $product->name . “\n”; }
This compares products by price. The callback checks the price property. Products with unique prices in the first array are returned.
Perform a case-insensitive comparison of string arrays.
case_insensitive.php
<?php
declare(strict_types=1);
function compareStrings(string $a, string $b): int { return strcasecmp($a, $b); }
$array1 = [“Apple”, “Banana”, “Cherry”]; $array2 = [“apple”, “banana”, “grape”];
$result = array_udiff($array1, $array2, ‘compareStrings’);
print_r($result);
This compares strings ignoring case. The callback uses strcasecmp. Only “Cherry” is different as the others match case-insensitively.
Compare multi-dimensional arrays based on specific elements.
multi_dimensional.php
<?php
declare(strict_types=1);
function compareArrays(array $a, array $b): int { return $a[‘id’] <=> $b[‘id’]; }
$users1 = [ [‘id’ => 1, ’name’ => ‘Alice’], [‘id’ => 2, ’name’ => ‘Bob’], [‘id’ => 3, ’name’ => ‘Charlie’] ];
$users2 = [ [‘id’ => 2, ’name’ => ‘Robert’], [‘id’ => 4, ’name’ => ‘David’] ];
$result = array_udiff($users1, $users2, ‘compareArrays’);
print_r($result); // Outputs users with IDs 1 and 3
This compares arrays by their ‘id’ elements. The callback checks the id key. Users with unique IDs in the first array are returned.
Compare one array against multiple other arrays simultaneously.
multiple_arrays.php
<?php
declare(strict_types=1);
function compareValues($a, $b): int { if (is_numeric($a) && is_numeric($b)) { return $a <=> $b; } return strcmp((string)$a, (string)$b); }
$mainArray = [1, ‘apple’, 3.14, true, ‘orange’]; $array2 = [1, 3.14]; $array3 = [‘apple’, ‘banana’];
$result = array_udiff($mainArray, $array2, $array3, ‘compareValues’);
print_r($result);
This compares against multiple arrays with mixed types. The callback handles both numeric and string comparisons. Only unique values remain.
Consistent Callbacks: Ensure callbacks return consistent comparison results.
Type Safety: Add type hints to callback parameters when possible.
Performance: For large arrays, optimize callback functions.
Readability: Use descriptive names for callback functions.
This tutorial covered the PHP array_udiff function with practical examples showing its usage for various comparison scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.