array_udiff、array_udiff_assoc、array_udiff_uassoc 使用方法
<?php // array_udiff 用自定义函数比较数组的差值(array_diff 使用内置函数)
// 使用该函数我们通过进行更复杂的比较 class Rectangle
{
public $width; public $height; public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
} $array1 = [
'a' => new Rectangle(1, 2),
'b' => new Rectangle(2, 3),
]; $array2 = [
'a' => new Rectangle(2, 3),
'c' => new Rectangle(3, 5),
]; // 键比较函数
function compare_key($key1, $key2) {
if ($key1 == $key2) {
return 0;
} return $key1 > $key2 ? 1 : -1;
} // 值比较函数
function compare_area(Rectangle $value1, Rectangle $value2) {
$area1 = $value1->width * $value1->height;
$area2 = $value2->width * $value2->height; if ($area1 == $area2) {
return 0;
}
return $area1 > $area2 ? 1 : -1;
} // 返回数据差集, 只比较值
var_dump(array_udiff($array1, $array2, 'compare_area')); // 返回数据差集, 同时检查键值(键的比较使用内置方法)
var_dump(array_udiff_assoc($array1, $array2, 'compare_area')); // 返回数据差集, 同时检查键值(键的比较使用自定义函数)
var_dump(array_udiff_uassoc($array1, $array2, 'compare_area', 'compare_key'));
array(1) {
["a"]=>
object(Rectangle)#1 (2) {
["width"]=>
int(1)
["height"]=>
int(2)
}
}
array(2) {
["a"]=>
object(Rectangle)#1 (2) {
["width"]=>
int(1)
["height"]=>
int(2)
}
["b"]=>
object(Rectangle)#2 (2) {
["width"]=>
int(2)
["height"]=>
int(3)
}
}
array(2) {
["a"]=>
object(Rectangle)#1 (2) {
["width"]=>
int(1)
["height"]=>
int(2)
}
["b"]=>
object(Rectangle)#2 (2) {
["width"]=>
int(2)
["height"]=>
int(3)
}
}
array_udiff、array_udiff_assoc、array_udiff_uassoc 使用方法的更多相关文章
- GeSHi Documentation
GeSHi Documentation Version 1.0.8.11 Authors: © 2004 - 2007 Nigel McNie © 2007 - 2012 Benny Baumann ...
- nodejs 的一些PHP函数库
http://locutus.io/php/ nodejs 的一些PHP函数库 PHP extensions in JavaScript array array_change_key_case arr ...
- Deformity PHP Webshell、Webshell Hidden Learning
目录 . 引言 . webshell原理介绍 . webshell的常见类型以及变种方法 . webshell的检测原理以及检测工具 . webshell隐藏反检测对抗手段 0. 引言 本文旨在研究W ...
- 用trie树实现输入提示功能,输入php函数名,提示php函数
参照刘汝佳的trie树 结构体 #include "stdio.h" #include "stdlib.h" #include "string.h&q ...
- PHP一句话木马研究
最近在研究PHP一句话后门,查阅了很多大佬的博客,并从中衍生出了一些可用的方法. 现总结如下: 方案一:回调函数 回调函数:Callback (即call then back 被主函数调用运算后会返回 ...
- javaSE27天复习总结
JAVA学习总结 2 第一天 2 1:计算机概述(了解) 2 (1)计算机 2 (2)计算机硬件 2 (3)计算机软件 2 (4)软件开发(理解) 2 (5) ...
- php array_udiff_uassoc比较数组的键值与值
php array_udiff_uassoc 用于带索引检查计算数组的差集,用回调函数比较数据和索引.本文章通过实例向大家介绍array_udiff_uassoc函数的使用方法.需要的码农可以参考一下 ...
- PHP中的数组方法及访问方法总结
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- PHP array_udiff_assoc() 函数
实例 比较两个数组的键名和键值(使用内建函数比较键名,使用用户自定义函数比较键值),并返回差集: <?phpfunction myfunction($a,$b){if ($a===$b){ret ...
随机推荐
- 【Python 开发】Python目录
目录: [Python开发]第一篇:计算机基础 [Python 开发]第二篇 :Python安装 [Python 开发]第三篇:python 实用小工具
- shell基础 -- 入门篇
shell 英文含义是“壳”,这是相对于内核来说的,shell 也确实就像是内核的壳,通常来说,所有对内核的访问都要经由 shell .同时,shell 还是一门功能强大的编程语言.shell 是 L ...
- Python基础灬补充(循环、格式化输出)
for循环&格式化输出 chinese_zodiac = '鼠牛虎兔龙蛇马羊猴鸡狗猪' for year in range(2000, 2013): print("%s年的生肖是:% ...
- 获取秒级时间戳和毫秒级时间戳---基于python
获取秒级时间戳和毫秒级时间戳 import timeimport datetime t = time.time() print (t) #原始时间数据print (int(t)) #秒级时间戳prin ...
- MySQL case when 使用
case when 自定义排序时的使用 根据 case when 新的 sort字段排序 case when t2.status = 4 and t2.expire_time>UNIX_TIME ...
- Play on Words(欧拉回路)
Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologi ...
- 希尔排序(java实现)
上篇blog中介绍的直接插入排序,希尔排序就是对直接插入排序的一个优化.比如有这么一种情况:对一个无序数组进行从小到大的排序,但是数组的最后一个位置的数是最小的,我们要把它挪到第一个位置,其他位置的都 ...
- 【每日scrum】NO.8
(1) 在图的设计过程中掌握了图的基本运算函数的算法的理解和程序的有效吸收,包括图的深度和广度优先的遍历,对图的联通分量的求解,图的最小生成树,图的拓扑排序,图的关键路径, (2)在迪杰斯特拉算法的基 ...
- android入门 — Activity启动模式
1.standard模式 standard模式是系统的默认启动方式,每次激活Activity都会创建Activity,并放在任务栈中. 系统不会在乎活动是否已经存在于返回栈中,每次启动都会创建该活动的 ...
- C++ auto_ptr智能指针的用法
C++中指针申请和释放内存通常采用的方式是new和delete.然而标准C++中还有一个强大的模版类就是auto_ptr,它可以在你不用的时候自动帮你释放内存.下面简单说一下用法. 用法一: std: ...