PHP ARRAY_UDIFF_ASSOC Compute the difference set instance _php instance of two arrays

Source: Internet
Author: User
Tags arrays

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

array_udiff_assoc-the difference set of an array with an index check to compare data with a callback function

Basic syntax:

Array Array_udiff_assoc (array $array 1, array $array 2 [, Array $ ...], callable $data _compare_func)
This comparison is done through the user-supplied callback function. If you think that the first argument is less than, equal to, or greater than the second parameter, 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 of a multidimensional array. Of course, you can use ARRAY_UDIFF_ASSOC ($array 1[0], $array 2[0], "Some_comparison_func"); To examine the deeper dimensions.

Parameter introduction

Parameters Description
Array1 Necessary. The first array.
Array2 Necessary. A second array.
MyFunction

Necessary. A string value that defines the callable comparison function.

When the first argument 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.

return value

ARRAY_UDIFF_ASSOC () returns an array that includes all values that are in array1 but not in any other parameter array. Note that unlike the Array_diff () and Array_udiff (), the key name is also used for comparison. The comparison of array data is performed using a user-supplied callback function. This is in contrast to the behavior of ARRAY_DIFF_ASSOC (), which is compared by internal functions.

Instance

<?php
class 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 (a),
  0 => new CR (P),
  1 => new CR (4), C16/>2 => New CR ( -15),
);
$b = Array (
  "0.2" => new CR (9),
  "0.5" => new CR (
  0 => New CR (3),
  1 => new CR (4), 
   2 => New CR ( -15),
);
$result = ARRAY_UDIFF_ASSOC ($a, $b, array (
  "Cr",
  "COMP_FUNC_CR"
));
Print_r ($result);

Run Result:

Array
(
  [0.1] => CR object
    (
      [priv_member:private] => 9
    )

  [0.5] => CR Object
    (
      [priv_member:private] =>
    )

  [0] => cr Object
    (
      [priv_member:private] =>)
)

The above example can see that the key value pair "1" => new CR (4) appears in two arrays and therefore is not in the output of this function.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.