1

Let's continue. Why array_udiff compares values of 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

3
  • ... to find the actual matches? Just like with your uassoc question? Commented Mar 4, 2015 at 15:13
  • @bwoebi , it's already known that 'b' > 'a' because $a already sorted. I see no pros from this repeated comparison. Commented Mar 4, 2015 at 15:17
  • Oh, sorry, I didn't pay enough attention here… Commented Mar 4, 2015 at 15:19

1 Answer 1

2

After the comparison of A[0] and B[0] it will skip all values in A that equal A[0], because B doesn't have that value; see here.

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

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

Output:

...
a : x
a : a <-- *
a : b
b : x
b : c
c : x
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.