PHP array_udiff_uassoc Function
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.
Basic Definition
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.
Basic array_udiff_uassoc Example
This shows a simple comparison of two arrays with custom comparison functions.
<?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.
Comparing Objects with Custom Logic
Compare arrays of objects using custom comparison functions for properties.
<?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.
Case-Insensitive String Comparison
Perform case-insensitive comparison for both keys and values.
<?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.
Multi-Dimensional Array Comparison
Compare multi-dimensional arrays with custom comparison logic.
<?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.
Complex Custom Comparison
Implement complex comparison logic combining multiple factors.
<?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.
Best Practices
- 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.
Source
PHP array_udiff_uassoc Documentation
This tutorial covered the PHP array_udiff_uassoc
function with
practical examples showing its usage for complex array comparisons.
Author
List all PHP Array Functions.