PHP Array_udiff_assoc Computes An example of a difference set of two arrays

Source: Internet
Author: User
The PHP Array_udiff () function is used to compare the key names and key values of two (or more) arrays, and to return a difference set. This comparison is done through a user-supplied callback function. If you think that the first parameter is less than, equal to, or greater than the second argument, you must return an integer less than 0, equal to zero, or greater than 0, respectively. This article explains the use of the Array_udiff () function through an example,

array_udiff_assoc-with index Check the difference set of the computed array, using the callback function to compare the data

Basic syntax:

Array Array_udiff_assoc (array $array 1, array $array 2 [, Array $ ...], callable $data _compare_func)
This comparison is done through a user-supplied callback function. If you think that the first parameter is less than, equal to, or greater than the second argument, you must return an integer less than 0, equal to zero, or greater than 0, respectively.

Note: Notice that this function examines only one dimension in a multidimensional array. Of course, can be used Array_udiff_assoc ($array 1[0], $array 2[0], "Some_comparison_func"); To check the deeper dimensions.

Parameter introduction

return value

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    ( C26/>[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.

Thank you for reading, hope to help everyone, thank you for the support of this site!

Related Article

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.