了解array_udiff()

I have the following simple function to try and understand the array_udiff() function

function udiffCompare( $value, $value2 )
{

echo $value . " - " . $value2 . "<br />";

}

$newArray   =   array(  

                    'value2' => 2, 
                    'value3' => 3, 
                    'value4' => 4
                );

$newArray2  =   array(  

                    'value2' => 2, 
                    'value3' => 3, 
                    'value4' => 4
                );

array_udiff( $newArray, $newArray2, 'udiffCompare' );

I would expect this to simply return:

2 - 2
3 - 3
4 - 4

However it returns:

3 - 2
4 - 3
3 - 2
4 - 3
4 - 4
4 - 3
4 - 3
3 - 2

This leads me to believe there is something I am really not understanding here about how array_udiff() works.

Even if I replace the echo statement above with:

if( $value == $value2 ) { return 1; } else { return 0; }

the outputted array is completely empty even though all the values passed to the function are equal.

Could someone shine a light please?

array_udiff computes the difference of the two arrays. That is all entries in $newArray which are not in $newArray2. In this case the result is an empty array because there is no difference.

The output you are seeing is because you are echoing each value that is being compared. The reason this is a larger list is because, in order to find out the difference array_udiff has to compare every value in $newArray with every value in $newArray2.

You didn't write proper callback function for array_udiff().

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Moreover mathematical difference of these 2 sample arrays you given is NULL That is why you will get empty result set if you use this function. Check it on array_diff();

The gray area shows the difference of two collections:

enter image description here