PHP function array_udiff() that compares the key values ​​of two arrays and returns the difference

黄舟
Release: 2023-03-17 08:46:01
Original
2622 people have browsed it

Example

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

<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");

$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>
Copy after login

Definition and usage

array_udiff() function is used to compare the key values ​​of two (or more) arrays and return the difference.

Note: This function uses a user-defined function to compare key values!

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

Syntax

array_udiff(array1,array2,array3...,myfunction)
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.
myfunctionRequired. 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 keys that are in the array being compared (array1), but not in any of the other argument arrays (array2 or array3, etc.).
PHP Version: 5.1.0+

更多实例

实例 1

比较三个数组的键值(使用用户自定义函数比较键值),并返回差集:

<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");

$result=array_udiff($a1,$a2,$a3,"myfunction");
print_r($result);

?>
Copy after login
<?php  
// array_udiff()  
class Obj  
{  
    private $code = 0;  
  
    public function construct($code)  
    {  
        $this->code = intval($code);  
    }  
  
    public function getCode()  
    {  
        return $this->code;  
    }  
  
    /** 
     * 该函数必须根据实际情况返回 0, 1, -1 这三种值,才能正确计算出差集 
     */  
    public static function cmp($a, $b)  
    {  
        if ($a->getCode() === $b->getCode()) return 0;  
        return $a->getCode() > $b->getCode() ? 1 : -1;  
    }  
  
    /** 
     * 此种形式是在《深入PHP面向对象模式与实践》一书看到的写法 
     * 经验证是无效的,具体在第十章讲解组合模式处 
     */  
    public static function cmp2($a, $b)  
    {  
        return $a === $b ? 0 : 1;  
    }  
}  
  
$e1 = new Obj(&#39;111&#39;);  
$e2 = new Obj(&#39;222&#39;);  
$e3 = new Obj(&#39;333&#39;);  
$e4 = new Obj(&#39;444&#39;);  
$e5 = new Obj(&#39;555&#39;);  
$e6 = new Obj(&#39;666&#39;);  
  
$arr = [$e1, $e2, $e3, $e4, $e5, $e6];  
$arr2 = [$e2, $e3, $e6];  
  
$arr = array_udiff($arr, $arr2, [&#39;Obj&#39;, &#39;cmp&#39;]);  
// $arr = array_udiff($arr, $arr2, [&#39;Obj&#39;, &#39;cmp2&#39;]); // cmp2 这个比较方法无法返回正确结果  
var_dump($arr);
Copy after login

array_diff 使用注意:

1. 返回差集中,键名保留不变

2. 两个单元仅在 (string) $elem1 === (string) $elem2 时被认为是相同的。也就是说,当字符串的表达是一样的时候。


类似的函数:

array_diff_assoc

array_diff_uassoc

array_diff_key

array_diff_ukey

array_udiff_assoc

array_udiff_uassoc

总结:

array_diff, array_udiff 仅根据 value 判断

array_diff_key, array_diff_ukey 仅根据 key 判断

array_diff_assoc, array_diff_uassoc, array_udiff_assoc, array_udiff_uassoc 根据 key和value 一起来判断

The above is the detailed content of PHP function array_udiff() that compares the key values ​​of two arrays and returns the difference. 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