PHP is used to compare the key names and key values of two arrays and return the difference set function Array_udiff_assoc ()

Source: Internet
Author: User

Instance

Compares the key names and key values of two arrays (using the built-in function to compare key names, using a user-defined function to compare key values), and returns the difference set:

<?phpfunction myfunction ($a, $b) {if ($a = = = $b) {return 0;} return ($a > $b) 1:-1;} $a 1=array ("a" and "Red", "B" and "Green", "C" and "Blue"), $a 2=array ("a" and "Red", "b" = "Blue", "c" = "green") ); $result =array_udiff_assoc ($a 1, $a 2, "MyFunction");p Rint_r ($result);? >

Definition and usage

The Array_udiff () function is used to compare the key names and key values of two (or more) arrays and return a difference set.

Note: This function uses the built-in function to compare key names and use user-defined functions to compare key values!

The function compares the key names and key values of two (or more) arrays, and returns an array of difference sets that contain all the key names and key values in the array being compared (array1), but not in any other parameter array (array2 or array3, and so on).

Grammar

ARRAY_UDIFF_ASSOC (array1,array2,array3...,myfunction)
parameters description
array1 required. The first array to compare with the other array.
array2 required. The array to compare with the first array.
array3,... optional. The other array to compare to the first array.
myfunction required. A string that defines the callable comparison function. If the first parameter is <, =, > The second argument, the corresponding comparison function must return an integer of < =, > 0.

Technical details

return value: Returns an array of difference sets that includes all the key names and key values in the array being compared (array1), but not in any other parameter array (array2 or array3, and so on).
PHP version: 5+

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.

Instance

<?phpclass CR {  private $priv _member;  function cr ($val) {    $this->priv_member = $val;  }  static function Comp_func_cr ($a, $b) {    if ($a->priv_member = = = $b->priv_member) return 0;    Return ($a->priv_member > $b->priv_member)? 1:-1;  }} $a = Array (  "0.1" = new CR (9),  "0.5" = new CR,  0 = new CR,  1 = new CR (4), 
  2 = new CR ( -15), $b = Array (  "0.2" = new CR (9),  "0.5" = new CR,  0 = new CR (3), c16/>1 = new CR (4),  2 = new CR ( -15), $result = Array_udiff_assoc ($a, $b, array (  "Cr",  "Comp_func _CR "));p Rint_r ($result);? >

Operation Result:

Array (  [0.1] = cr Object    (      [priv_member:private] = 9    )   [0.5] = cr Object    (      [ Priv_member:private]   [0] = = CR Object    (      [priv_member:private]    = +))

In the example above, you can see that the key value pair "1" and The new CR (4) appear in two arrays and therefore are not in the output of this function.

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.