Questions about Array_udiff_assoc and Array_intersect_ukey?

Source: Internet
Author: User

There is no back-back function at home, the following problems occur? Help! Thank you ~

Excuse me! 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:
Health and health values use two different callback functions at the same time, a positive value is 1, a positive number is 0
A positive or negative value is output or returned, and 0 is not output.
I set the key to the positive value should be output, and its Val set to 0 it will not output.
Do they not conflict? What should I do to explain this?
And how is the string more than and less than the call? Does it count in bytes? Thanks, man.

Reply content:

There is no back-back function at home, the following problems occur? Help! Thank you ~

Excuse me! 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:
Health and health values use two different callback functions at the same time, a positive value is 1, a positive number is 0
A positive or negative value is output or returned, and 0 is not output.
I set the key to the positive value should be output, and its Val set to 0 it will not output.
Do they not conflict? What should I do to explain this?
And how is the string more than and less than the call? Does it count in bytes? Thanks, man.

1) can ARRAY_UDIFF_ASSOC and Array_diff_uassoc really be used as aliases? @ yee Red Childe Sorry, you seem to have the code inside the two function name is wrong. But have to spit the title of the master directly to put the code ah, want to test the answer to the person must also have to knock again ...

No, you can make an alias, which is explained in the PHP manual. Similar to the Array_diff_Assoc named function has 4, where u is the indication whether to accept the user to pass the callback function meaning. Diff represents the comparison of an array value, and Assoc indicates whether the key value is also compared (that is, both key and value are considered identical). diff preceded by a custom comparison function that supports the user passing in the array value, assoc a custom comparison function that supports the user passing in a number of build values.

You output the same results only because your processing results are exactly the same, we judge that the function alias can not be judged by the output results. The ARRAY_UDIFF_ASSOC callback is passed to the all function's $v1, $v 2 is the value of the array (Green,grown ... While the ARRAY_DIFF_UASSOC callback is passed to the all function's $v1, $v 2 is the key value of the array (a, B, C ...).

2) Array_intersect_key and Array_intersect_ukey are queries that have cross-key values and return, the difference being that the latter uses a custom callback function.

As the red Childe said, the return is different because you wrote the callback function wrong. So why was it wrong? Simply put, because PHP is in the bottom of the comparison, the first two arrays of the keys to do a quick sort, then if the first set of keys inside the 3rd value is greater than the second set of keys inside the 2nd value (the callback function is return 1; greater than), The first 2 values of the second group are then skipped directly after the next comparison of the 4th values. To improve efficiency, this is why the callback function has to return 1, 0, 13 different values.

First spit groove, Sao year is fine to look at the handbook AH.

array_diff_assocAndarray_udiff_assoc

See the wrong function, should be array_diff_uassoc and array_udiff_assoc the difference, as @samoay said, is a comparison key, one is the comparison value. This is not really seen in the Chinese description of the manual, but the example is very clear. If you want to know the details, you can see the answer @samoay.

----------I'm the wrong answer split line----------

These two are obviously different functions, the biggest difference being that array_udiff_assoc you can use the third parameter to customize the way you compare. Without the array_diff_assoc parameters of this callback function, your example is obviously used incorrectly. Quoting the Official handbook, that's what it says:

ARRAY_UDIFF_ASSOC () returns an array that contains all the values in array1 but not in any other parameter array. Note that unlike Array_diff () and Array_udiff (), the key name is also used for comparison. The comparison of the array data is done with the user-supplied callback function. This is in contrast to the behavior of ARRAY_DIFF_ASSOC (), which is compared with an intrinsic function. --array_udiff_assoc

To say that this is the result of a consistent problem, can only be said that this is because you write the callback function and the system comes from the same way to determine the result of the same, not to say that the two functions is a function. There are also many functions of the same type, such as sort and usort .
----------the wrong answer is over----------

array_intersect_keyAndarray_intersect_ukey

Here is more need to spit the groove you, the reason why you do array_intersect_ukey not return the result is very simple, is your Keys function is not written right. Please see what the Official Handbook says:

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

See clearly, need to return three integer values separately OK, and array_udiff_assoc is not the same, do not arbitrarily apply. The actual test, if the correct value is returned is normal, 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"));//返回 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.