Under the hood, PHP's array_u*() functions perform sorting to improve filtration performance. Values will be compared, two at a time, until all values are sorted, then PHP can efficiently determine which elements should be kept/removed.
From the manual:
Caution The sorting callback must handle any value from any array in any order, regardless of the order they were originally provided. This is because each individual array is first sorted before being compared against other arrays.
In your script, you are echoing the function arguments, but you are not returning any meaningful result (and array_u*() functions all expects the result of a 3-way comparison). This means that all compared pairs return 0 -- no tie breaks occur and therefore no performance boost. Because 0 evaluations mean "remove the element" with array_udiff(), all elements are removed.
In contrast, if you implement and return a 3-way evaluation for each pair of values, you will see different echoed pairs because tie breaks are required.
Also notice that the first and second arguments are not exclusively pulled from the input array with the corresponding position. In other words, the $a and $b values may come from any input array. This is important because array_diff() and array_intersect() functions can receive two or more input arrays.
Here's a demonstration with different input values to give better clarity. Demo
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
function udiffCompare($a, $b)
{
echo "Compare $a and $b\n";
}
var_export(
array_udiff($array1, $array2, 'udiffCompare')
);
echo "\n---\n";
function udiffCompare2($a, $b)
{
echo "Compare $a and $b\n";
return $a <=> $b;
}
var_export(
array_udiff($array1, $array2, 'udiffCompare2')
);
Output:
Compare 1 and 2
Compare 2 and 3
Compare 4 and 5
Compare 5 and 6
Compare 1 and 4
Compare 1 and 2
Compare 2 and 3
array (
)
---
Compare 1 and 2
Compare 2 and 3
Compare 4 and 5
Compare 5 and 6
Compare 1 and 4
Compare 1 and 2
Compare 2 and 4
Compare 2 and 3
Compare 3 and 4
array (
0 => 1,
1 => 2,
2 => 3,
)
int callback ( mixed $a, mixed $b )should be its signature. Though I'd sayarray_maporlistis a better fit for your use-case