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.
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
<?php
function key_compare_func($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $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
)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
