Question about array_udiff_assoc and array_intersect_ukey?

Source: Internet
Author: User
There is no back-to-back function at home. The following problem occurs? Help! Thank you ~ Sorry! I also want to ask: {code ...} in the preceding example, the Health name and health value use two different callback functions at the same time. A positive value is 1, and a positive value is 0 or a negative value before it is output or returned. If the positive value is 0, it will not be lost...

There is no back-to-back function at home. The following problem occurs? Help! Thank you ~

Sorry! I would also like to ask:

function k($k1,$k2){    if($k1>$k2){        return 1;    }elseif($k1<$k2){        return -1;    }else{        return 0;    }}function v($v1,$v2){    if($v1>$v2){        return -1;    }elseif($v1<$v2){        return 0;    }else{        return 0;    }}$a = array(0=>"Cat",1=>"Dog",2=>"Horse");$b = array(2=>"Bird",3=>"Rat",4=>"Fish");print_r(array_udiff_uassoc($a,$b,'k','v'));

The above example:
The Health name and health value use two different callback functions at the same time. One positive value is 1, and the other positive value is 0.
Positive or negative values are output or returned, and 0 is not output.
I set the positive value of the key to be output, and set its val to 0, so it will not output.
Will they not conflict? Why should I explain this?
In addition, how is a string greater than or less? Is it calculated by byte? Thank you!

Reply content:

There is no back-to-back function at home. The following problem occurs? Help! Thank you ~

Sorry! I would also like to ask:

function k($k1,$k2){    if($k1>$k2){        return 1;    }elseif($k1<$k2){        return -1;    }else{        return 0;    }}function v($v1,$v2){    if($v1>$v2){        return -1;    }elseif($v1<$v2){        return 0;    }else{        return 0;    }}$a = array(0=>"Cat",1=>"Dog",2=>"Horse");$b = array(2=>"Bird",3=>"Rat",4=>"Fish");print_r(array_udiff_uassoc($a,$b,'k','v'));

The above example:
The Health name and health value use two different callback functions at the same time. One positive value is 1, and the other positive value is 0.
Positive or negative values are output or returned, and 0 is not output.
I set the positive value of the key to be output, and set its val to 0, so it will not output.
Will they not conflict? Why should I explain this?
In addition, how is a string greater than or less? Is it calculated by byte? Thank you!

1)Can array_udiff_assoc and array_diff_uassoc be used as aliases?@ Sorry, you seem to have read the two function names in the code wrong. However, I have to say that the subject has to directly paste the code. The person who wants to test the answer must try again...

No. If you can create an alias, the php manual describes it. Similar to array _Diff _There are four functions named by assoc, where u indicates whether to accept the callback function passed by the user. Diff indicates the comparison of array values, and assoc indicates whether the key values are also compared (that is, the keys and values are the same before they are considered the same ). In front of diff, there is a user-defined comparison function that allows users to pass in array values. In front of assoc, a user-defined comparison function that allows users to pass in number sets.

The result you output is the same only because your processing results are exactly the same. We cannot judge the function alias Based on the output results. $ V1 and $ v2 passed by array_udiff_assoc callback to the All function are array values (green, grown ...); the $ v1 and $ v2 that array_diff_uassoc calls back to All functions are the key values of the array (a, B, c ...).

2)Array_intersect_key and array_intersect_ukeyThey are all returned when the query has a cross key value. The difference is that the latter uses a custom callback function.

As @ Yi Hongyi said, the difference in the returned result is that you wrote the callback function incorrectly. So why is it wrong? Simply put, because at the bottom layer of PHP, the keys of the two arrays are first sorted in a fast order, if the 3rd values in the first group of keys are greater than the 2nd values in the second group of keys (callback functionreturn 1;If the value is greater than), then when we compare the first group of 4th values, we will directly skip the first two values of the second group. To improve efficiency, this is why the callback function must return three different values-1, 0, and 1.

Let's talk about it in the beginning. Cool will have a good look at the manual.

array_diff_assocAnd array_udiff_assoc

Wrong function. It should bearray_diff_uassocAndarray_udiff_assocAs @ samoay said, there is a comparison key and a comparison value. This is not very obvious in the Chinese description of the Manual, but the example is very clear. For more information, see @ samoay.

---------- I am the dividing line of incorrect answers ----------

These two functions are obviously different. The biggest difference is thatarray_udiff_assocYou can use the third parameter to customize the comparison method. Whilearray_diff_assocThere is no parameter for this callback function. It is obviously incorrect in your example. The following is the reference to the official manual:

Array_udiff_assoc () returns an array containing all values in the array of array1 but not any other parameters. Note that, unlike array_diff () and array_udiff (), the key name is also used for comparison. The array data is compared using the callback function provided by the user. In this regard, the behavior of array_diff_assoc () is the opposite. The latter is compared with internal functions. -- Array_udiff_assoc

To say that the results here are consistent, you can only say that this is because the callback function you write is exactly the same as the results of the system's built-in discriminant method. It cannot be said that the two functions are one function. There are also many functions of the same type, suchsortAndusort.
---------- Incorrect answer ended ----------

array_intersect_keyAnd array_intersect_ukey

Here we need to talk about you.array_intersect_ukeyThe reason for not returning results is simple. It's yours.KeysIsn't the function written correctly. See what the official manual says:

Key_compare_func
When the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0.
-- Array_intersect_ukey

It is clear that three integers need to be returned, respectively.array_udiff_assocThey are different. do not apply them as needed. It is normal if the correct value is returned. The following is the sample code I tested:


  'Dog ', 1 => 'cat', 2 => 'horse'); $ B = array (3 => 'rat', 1 => 'bird ', 5 => 'monkey'); function keys ($ k1, $ k2) {if ($ k1> $ k2) return 1; else if ($ k1 <$ k2) return-1; else return 0;} print_r (array_intersect_key ($ a, $ B); print_r (array_intersect_ukey ($ a, $ B, "keys ")); // returns Array ([1] => CAT) Array ([1] => CAT)

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.