PHP array_udiff_uassoc function tutorial shows how to compute array difference with callbacks in PHP. Learn array_udiff_uassoc with examples.
last modified March 13, 2025
The PHP array_udiff_uassoc function computes the difference of arrays with additional index check, using callbacks for both data and index comparison. It’s useful for complex array comparisons.
The array_udiff_uassoc function compares arrays by values and keys. It uses two callback functions - one for value comparison and another for key comparison.
Syntax: array_udiff_uassoc(array $array1, array $array2, …, callable $value_compare_func, callable $key_compare_func): array. Returns values from array1 not present in other arrays.
This shows a simple comparison of two arrays with custom comparison functions.
basic_array_udiff_uassoc.php
<?php
declare(strict_types=1);
function compareValues($a, $b): int { return $a <=> $b; }
function compareKeys($a, $b): int { return strcasecmp($a, $b); }
$array1 = [“a” => 1, “b” => 2, “c” => 3]; $array2 = [“A” => 1, “B” => 5, “C” => 3];
$result = array_udiff_uassoc($array1, $array2, ‘compareValues’, ‘compareKeys’);
print_r($result);
This compares arrays case-insensitively for keys and normally for values. Only the element with key “b” and value 2 is in array1 but not array2.
Compare arrays of objects using custom comparison functions for properties.
object_comparison.php
<?php
declare(strict_types=1);
class Product { public function __construct( public string $name, public float $price ) {} }
function compareProducts($a, $b): int { return $a->price <=> $b->price; }
function compareKeys($a, $b): int { return strcmp($a, $b); }
$products1 = [ “p1” => new Product(“Laptop”, 999.99), “p2” => new Product(“Phone”, 699.99) ];
$products2 = [ “p1” => new Product(“Tablet”, 399.99), “p3” => new Product(“Monitor”, 299.99) ];
$result = array_udiff_uassoc($products1, $products2, ‘compareProducts’, ‘compareKeys’);
print_r($result); // Outputs both products from $products1
This compares products by price and keys normally. Since no products in array1 have matching prices in array2, both are returned in the result.
Perform case-insensitive comparison for both keys and values.
case_insensitive.php
<?php
declare(strict_types=1);
function compareValues($a, $b): int { return strcasecmp($a, $b); }
function compareKeys($a, $b): int { return strcasecmp($a, $b); }
$array1 = [“Name” => “John”, “Age” => “30”]; $array2 = [“name” => “JOHN”, “age” => “25”];
$result = array_udiff_uassoc($array1, $array2, ‘compareValues’, ‘compareKeys’);
print_r($result);
This performs case-insensitive comparison for both keys and values. Only the Age element differs between arrays when compared this way.
Compare multi-dimensional arrays with custom comparison logic.
multi_dimensional.php
<?php
declare(strict_types=1);
function compareValues($a, $b): int { return $a[‘score’] <=> $b[‘score’]; }
function compareKeys($a, $b): int { return $a <=> $b; }
$students1 = [ 101 => [’name’ => ‘Alice’, ‘score’ => 85], 102 => [’name’ => ‘Bob’, ‘score’ => 90] ];
$students2 = [ 101 => [’name’ => ‘Alice’, ‘score’ => 80], 103 => [’name’ => ‘Charlie’, ‘score’ => 95] ];
$result = array_udiff_uassoc($students1, $students2, ‘compareValues’, ‘compareKeys’);
print_r($result); // Outputs both students from $students1
This compares student records by their score values. Since no students in array1 have matching scores in array2, both are included in the result.
Implement complex comparison logic combining multiple factors.
complex_comparison.php
<?php
declare(strict_types=1);
function compareValues($a, $b): int { $scoreA = $a[‘points’] * $a[‘multiplier’]; $scoreB = $b[‘points’] * $b[‘multiplier’]; return $scoreA <=> $scoreB; }
function compareKeys($a, $b): int { return strlen($a) <=> strlen($b); }
$data1 = [ “user1” => [‘points’ => 10, ‘multiplier’ => 2], “longuser” => [‘points’ => 5, ‘multiplier’ => 3] ];
$data2 = [ “usr” => [‘points’ => 10, ‘multiplier’ => 2], “user” => [‘points’ => 5, ‘multiplier’ => 4] ];
$result = array_udiff_uassoc($data1, $data2, ‘compareValues’, ‘compareKeys’);
print_r($result);
This compares data by calculated score (points * multiplier) and keys by length. Only the first element differs when compared this way.
Consistent Callbacks: Ensure comparison functions return consistent results.
Type Safety: Add type hints to callback functions for robustness.
Performance: Optimize callbacks for large array comparisons.
Readability: Use descriptive names for callback functions.
PHP array_udiff_uassoc Documentation
This tutorial covered the PHP array_udiff_uassoc function with practical examples showing its usage for complex array comparisons.
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.