Backend Development 2 min read

PHP array_diff_uassoc() – Compute Array Difference with User-Defined Key Comparison

The article explains how PHP's array_diff_uassoc() function compares arrays using both values and user‑defined key comparison callbacks, describes its parameters and return value, and provides a complete example with code and expected output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_diff_uassoc() – Compute Array Difference with User-Defined Key Comparison

The PHP function array_diff_uassoc() compares two or more arrays and returns the values from the first array that are not present in any of the other arrays, using both values and keys for comparison, with a user‑defined callback to compare the keys.

Parameters

array $array1 – The array to compare.

array $array2 – The array to compare against.

... – Additional arrays to compare.

callable $key_compare_func – A function that returns <0, 0, >0 when the first key is less than, equal to, or greater than the second key.

Return value

Returns an array containing all entries from $array1 whose keys and values are not present in any of the other arrays.

Example

$b) ? 1 : -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
?>

Output

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)
BackendPHParraycallbackfunctionDiffassociative array
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.