PHP Array_udiff's working principle is really confusing, especially the callback function,
$arr1 = array('m1'=>1, 'm2'=>5, 'm3'=>3); $arr2 = array('n2'=>4, 'n2'=>5, 'n3'=>6); var_dump(array_udiff($arr1, $arr2, function($a, $b){ print_r($a.'~~~~'.$b.'
'); if($a>$b) return 1; else return 0; }));
Each time the parameter of the callback function is what I print it out, it is not as expected that each element value of $arr 1 is directly compared to the value of each element of $ARR2, but
5~~~~13~~~~51~~~~36~~~~51~~~~51~~~~33~~~~5
The callback function returns the result of what to do, solve
Finally spit groove, the results of the online search all of him from Www3school and php.net copy down, really can not endure,
Reply content:
PHP Array_udiff's working principle is really confusing, especially the callback function,
$arr1 = array('m1'=>1, 'm2'=>5, 'm3'=>3); $arr2 = array('n2'=>4, 'n2'=>5, 'n3'=>6); var_dump(array_udiff($arr1, $arr2, function($a, $b){ print_r($a.'~~~~'.$b.'
'); if($a>$b) return 1; else return 0; }));
Each time the parameter of the callback function is what I print it out, it is not as expected that each element value of $arr 1 is directly compared to the value of each element of $ARR2, but
5~~~~13~~~~51~~~~36~~~~51~~~~51~~~~33~~~~5
The callback function returns the result of what to do, solve
Finally spit groove, the results of the online search all of him from Www3school and php.net copy down, really can not endure,
Let's not take care of how the algorithm is implemented internally, what we callback need to achieve in the回调函数的第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。
When the landlord example changes to the following, the normal comparison function can be achieved:
$arr1 = array('m1'=>1, 'm2'=>5, 'm3'=>3);$arr2 = array('n2'=>4, 'n2'=>5, 'n3'=>6);var_dump(array_udiff($arr1, $arr2, function($a, $b){print_r($a.'~~~~'.$b.'
'); if($a>$b){ return 1; } else if($a == $b) { return 0; } else if($a < $b) { return -1; }}));
The callback function simply implements how to determine whether the two values passed in are equal or greater than the less-than relationship. Both values are elements of the array. But don't get me wrong.第一个参数是第一个数组的值,第二个参数是第二个数组的值。
call_backThere are 2 functions in this function,
* Sort sorts each parameter in the Array_udiff and returns the array after the hash
* Filter compares the first array to other arrays to filter out the values to be returned
P.S. I'm sure the process is correct. Reference Php:array_udiff ()
In your case,
$arr1 = array('m1'=>1, 'm2'=>5, 'm3'=>3);$arr2 = array('n2'=>4, 'n2'=>5, 'n3'=>6); //n2=>4应该是n1吧?输出中的前4项5~~~~13~~~~51~~~~36~~~~5就是在做排序拍完序之后,会把第一数组中的每一个项去其他数组中检查,若不存在则将该项放入返回数组中。但是从输出的内容上看有点诡异,我也不明白!囧。
The Array_diff itself is the difference set of the computed array. Doesn't seem to support callback functions, does it?
http://cn2.php.net/manual/zh/function.array-diff.php