"; print_r(array_udiff($a,$b,func...">

I am confused about the problem of how to use array_udiff

$a = array(1,2,3,4,5); $b = array(1,6,3,4,5); echo "<pre>"; print_r(array_udiff($a,$b,function($a,$b){ return ($a === $b)? 0:1; })); 

As the manual says: It should return an array containing all array1 values ​​that are not present in any of the other arguments. If you compare only two arrays, I think it will be like array_diff() .
I expect this to return:

 Array ( [0] => 2 ) 

However, it returns:

 Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 5 ) 
+2
source share
1 answer

For the value_compare_func array_udiff() parameter, "The comparison function must return an integer less than, equal to or greater than zero, if the first argument is considered correspondingly less, equal to or greater than the second."

http://us1.php.net/manual/en/function.array-udiff.php

This fixes the problem and returns an array with element 2 .

 $a = array(1,2,3,4,5); $b = array(1,6,3,4,5); echo "<pre>"; print_r(array_udiff($a,$b,function($a,$b){ if ($a < $b) { return -1; } elseif ($a > $b) { return 1; } else { return 0; }; })); 
+2
source

All Articles