I have the following simple function to try and understand the array_udiff()
function.
function udiffCompare( $value, $value2 )
{
echo "$value - $value2<br />";
}
$newArray = ['value2' => 2, 'value3' => 3, 'value4' => 4];
$newArray2 = ['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
.