How phparray_udiff works

Source: Internet
Author: User
Php array_udiff does not really understand the working principle, especially the callback function, {code ...} what is the parameter of each callback function? I print it out, but not as expected. Each element value of $ arr1 is directly compared with each element value of $ arr2, but {code ...} callback... the working principle of php array_udiff is hard to understand, 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; }));

What is the parameter of each callback function? I print it out, but not as expected. Every element value of $ arr1 is directly compared with every element value of $ arr2.

5~~~~13~~~~51~~~~36~~~~51~~~~51~~~~33~~~~5

What is the result returned by the callback function?

Finally, I spoke about it. I copied all the results on the Internet from www3school and php.net.

Reply content:

The working principle of php array_udiff is hard to understand, 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; }));

What is the parameter of each callback function? I print it out, but not as expected. Every element value of $ arr1 is directly compared with every element value of $ arr2.

5~~~~13~~~~51~~~~36~~~~51~~~~51~~~~33~~~~5

What is the result returned by the callback function?

Finally, I spoke about it. I copied all the results on the Internet from www3school and php.net.

Do not worry about how the comparison algorithm is implemented internally.callbackTo be implemented inWhen the first parameter of the callback function is smaller than, equal to, or greater than the second parameter, the comparison function must return an integer smaller than, equal to, or greater than 0.
When the example of the landlord is changed 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 only needs to determine whether the two values passed in are equal to or greater than or less. Both values are elements of the array. But do not misunderstandThe first parameter is the value of the first array, and the second parameter is the value of the second array.

call_backThis function has two functions,
* Sort each parameter in array_udiff and return the array after hash.
* Filter and compare the first array with other arrays to filter out the values to be returned
P.S. I'm sure this process is correct. Refer to PHP: array_udiff ()

In your example

$ Arr1 = array ('m1 '=> 1, 'm2' => 5, 'm3 '=> 3); $ arr2 = array ('n2' => 4, 'n2 '=> 5, 'n3' => 6); // n2 => 4 should it be n1? 5 ~~~~ of the first 4 items in the output ~~~~ 13 ~~~~ 51 ~~~~ 36 ~~~~ 5. After sorting and order, each item in the first array is checked in other arrays. If not, this item is placed in the returned array. But it seems a little strange from the output content, and I don't understand it! .

Array_diff is the difference set of the calculated array. Does it seem that callback functions are not supported?
Http://cn2.php.net/manual/zh/function.array-diff.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.