Trying to understand the behavior of array_udiff

Let's continue. Why does array_udiff compare the values ​​of the first array after sorting ?

$compare = function($a, $b) use(&$iteration_count)
    {
    echo("$a : $b\n");
    $iteration_count++;
    return strcmp($a, $b);
    };

$a = array('a', 'b', 'c');
$b = array('x', 'y', 'z');


$iteration_count = 0;
echo "array_udiff:" . json_encode(array_udiff($a, $b, $compare)) . "\n";
echo "iterations: $iteration_count\n\n";

Output

b : a  // sorting $a started
c : b   
y : x  // sorting $b started
z : y
a : x  // comparison started
a : b  //                    -- what for?
b : x
b : c  //                    -- what for?
c : x
array_udiff:["a","b","c"]
iterations: 9

http://3v4l.org/3Me8o#v500

+1
source share
1 answer

After comparison A[0], B[0]he will skip all the values ​​in Aequal A[0], because he Bdoes not have this value; see here .

To do this, he must compare at least A[1]with A[0]; you can observe this behavior by making a small change in the first array:

$a = array('a', 'a', 'b', 'c');

Conclusion:

...
a : x
a : a <-- *
a : b
b : x
b : c
c : x
+2
source

All Articles