Php array_udiff_assoc calculates the difference set instances of two arrays. arrayudiffassoc

Source: Internet
Author: User

Php array_udiff_assoc calculates the difference set instances of two arrays. arrayudiffassoc

The php array_udiff () function is used to compare the key names and key values of two (or more) arrays and return the difference set. This comparison is performed through the callback function provided by the user. If the first parameter is considered to be less than, equal to, or greater than the second parameter, an integer less than zero, equal to zero, or greater than zero must be returned. This article describes how to use the array_udiff () function through examples,

Array_udiff_assoc-calculate the difference set of the array with index check and use the callback function to compare data

Basic Syntax:

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

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

Parameter Introduction

Parameters Description
Array1 Required. The first array.
Array2 Required. The second array.
Myfunction

Required. String value, which defines the comparison function that can be called.

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

Return Value

Array_udiff_assoc () returns an array containing all values in the array of array1 but not any other parameters. Note that, unlike array_diff () and array_udiff (), the key name is also used for comparison. The array data is compared using the callback function provided by the user. In this regard, the behavior of array_diff_assoc () is the opposite. The latter is compared with internal functions.

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(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);?> 

Running 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, we can see that the key-Value Pair "1" => new cr (4) appears in both arrays at the same time, so it is not in the output of this function.

Thank you for reading this article. I hope it will 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.