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

黄舟
Release: 2023-03-17 08:50:01
Original
1719 people have browsed it

Example

Compare the key names and key values ​​of two arrays (use user custom function for comparison), and return the difference set:

<?php
function myfunction_key($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$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);
?>
Copy after login

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)
Copy after login
ParametersDescription
array1 Required. The first array to compare with other arrays.
array2Required. The array to compare to the first array.
array3,...Optional. Additional array to compare with the first array.
myfunction_keyRequired. The name of the user-defined function used to compare array key names.
A string that defines a callable comparison function. If the first argument is <, =, > and the second argument is, the corresponding comparison function must return an integer of <, =, > 0.
myfunction_valueRequired. 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 <, =, > and the second argument is, the corresponding comparison function must return an integer of <, =, > 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:

<?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;
    }
    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);
?>
Copy after login

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
        )
)
Copy after login
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 functions must 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template