Home  >  Article  >  Backend Development  >  php array_udiff_assoc calculates the difference set example of two arrays

php array_udiff_assoc calculates the difference set example of two arrays

高洛峰
高洛峰Original
2017-01-17 13:59:301410browse

php array_udiff() function is used to compare the key names and key values ​​of two (or more) arrays and return the difference. This comparison is performed through a user-supplied callback function. If the first argument is considered less than, equal to, or greater than the second argument, an integer less than zero, equal to zero, or greater than zero must be returned, respectively. This article explains how to use the array_udiff() function through examples,

array_udiff_assoc - calculates the difference set of the array with index check, and uses the callback function to compare the data

Basic syntax:

array array_udiff_assoc ( array $array1 , array $ array2 [, array $ ... ], callable $data_compare_func )
This comparison is performed through a user-provided callback function. If the first argument is considered less than, equal to, or greater than the second argument, an integer less than zero, equal to zero, or greater than zero must be returned, respectively.

Note: Note that this function only checks one dimension of the multi-dimensional array. Of course, you can use array_udiff_assoc($array1[0], $array2[0], "some_comparison_func"); to check deeper dimensions.

Parameter introduction

php array_udiff_assoc 计算两个数组的差集实例

Return value

array_udiff_assoc() returns an array that includes all values ​​in array1 but not in any other parameter array. Note that unlike array_diff() and array_udiff(), key names are also used for comparison. Comparison of array data is performed using user-supplied callback functions. This is the opposite behavior to array_diff_assoc(), which uses an internal function for comparison.

Example

<?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(12) ,
  0 => new cr(23) ,
  1 => new cr(4) ,
  2 => new cr(-15) ,
);
$b = array(
  "0.2" => new cr(9) ,
  "0.5" => new cr(22) ,
  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] => 12
    )
 
  [0] => cr Object
    (
      [priv_member:private] => 23
    )
)

In the above example, you can see that the key-value pair "1" => new cr(4) appears in both arrays at the same time, so it is not included in this example in the output of the function.

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more php array_udiff_assoc related articles on calculating the difference set of two arrays, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn