Home>Article>Backend Development> PHP uses two user-defined key name comparison functions array_udiff_uassoc()

PHP uses two user-defined key name comparison functions array_udiff_uassoc()

黄舟
黄舟 Original
2017-11-09 09:32:24 1654browse

Example

Compare the key names and key values of two arrays (use usercustom functionfor comparison), and return the difference set:

$b)?1:-1; } function myfunction_value($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"green","c"=>"green"); $result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value"); print_r($result); ?>

Definition and usage

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

Note: This function uses two user-defined functions for comparison; the first function compares key names, and the second function compares key values!

This function compares the key names and key values of two (or more) arrays, and returns a difference array, which includes everything in the compared array (array1), but not in any other The key name and key value in the parameter array (array2 or array3, etc.).

Syntax

array_udiff_uassoc(array1,array2,array3...,myfunction_key,myfunction_value)
Parameters Description
array1 Required. The first array to compare with other arrays.
array2 Required. The array to compare to the first array.
array3,... Optional. Additional array to compare with the first array.
myfunction_key Required. The name of the user-defined function used to compare array key names.
Astringthat defines a callable comparison function. If the first argument is f2c570bc5a616fb55b90df8c3566974f and the second argument is, the corresponding comparison function must return an integer of f2c570bc5a616fb55b90df8c3566974f 0.
myfunction_value Required. The name of the user-defined function used to compare array key values.
A string that defines a callable comparison function. If the first argument is f2c570bc5a616fb55b90df8c3566974f and the second argument is, the corresponding comparison function must return an integer of f2c570bc5a616fb55b90df8c3566974f 0.

Technical details

Return value: Returns a difference array, which includes Returns all key names and values that are in the compared array (array1) but not in any other parameter array (array2 or array3, etc.).
PHP version: 5+
##Instance:

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; } static function comp_func_key($a, $b) { if ($a === $b) return 0; return ($a > $b) ? 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_uassoc($a, $b, array( "cr", "comp_func_cr" ) , array( "cr", "comp_func_key" )); 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, 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. Remember that two

callback functionsmust be provided.

The above is the detailed content of PHP uses two user-defined key name comparison functions array_udiff_uassoc(). For more information, please follow other related articles on 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