首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

array_udiff

(PHP 5, PHP 7)

array_udiff - 通过使用回调函数进行数据比较来计算数组的差异

描述

代码语言:javascript
复制
array array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

通过使用回调函数进行数据比较来计算数组的差异。这与array_diff()不同,它使用内部函数来比较数据。

参数

array1

第一个数组。

array2

第二个数组。

value_compare_func

回调比较函数。

如果第一个参数分别小于,等于或大于第二个参数,则比较函数必须返回小于,等于或大于零的整数。请注意,在PHP 7.0.0之前,此整数必须在-2147483648到2147483647之间。

代码语言:javascript
复制
int callback ( mixed $a, mixed $b )

返回值

返回一个数组,其中包含在array1但不包含在其他任何参数中的所有值。

例子

示例#1使用stdClass对象的array_udiff()示例

代码语言:javascript
复制
<?php
//?Arrays?to?compare
$array1?=?array(new?stdclass,?new?stdclass,
????????????????new?stdclass,?new?stdclass,
???????????????);

$array2?=?array(
????????????????new?stdclass,?new?stdclass,
???????????????);

//?Set?some?properties?for?each?object
$array1[0]->width?=?11;?$array1[0]->height?=?3;
$array1[1]->width?=?7;??$array1[1]->height?=?1;
$array1[2]->width?=?2;??$array1[2]->height?=?9;
$array1[3]->width?=?5;??$array1[3]->height?=?7;

$array2[0]->width?=?7;??$array2[0]->height?=?5;
$array2[1]->width?=?9;??$array2[1]->height?=?2;

function?compare_by_area($a,?$b)?{
????$areaA?=?$a->width?*?$a->height;
????$areaB?=?$b->width?*?$b->height;
????
????if?($areaA?<?$areaB)?{
????????return?-1;
????}?elseif?($areaA?>?$areaB)?{
????????return?1;
????}?else?{
????????return?0;
????}
}

print_r(array_udiff($array1,?$array2,?'compare_by_area'));
?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
    [0] => stdClass Object
        (
            [width] => 11
            [height] => 3
        )

    [1] => stdClass Object
        (
            [width] => 7
            [height] => 1
        )

)

示例#2使用DateTime对象的array_udiff()示例

代码语言:javascript
复制
<?php
class?MyCalendar?{
????public?$free?=?array();
????public?$booked?=?array();

????public?function?__construct($week?=?'now')?{
????????$start?=?new?DateTime($week);
????????$start->modify('Monday?this?week?midnight');
????????$end?=?clone?$start;
????????$end->modify('Friday?this?week?midnight');
????????$interval?=?new?DateInterval('P1D');
????????foreach?(new?DatePeriod($start,?$interval,?$end)?as?$freeTime)?{
????????????$this->free[]?=?$freeTime;
????????}
????}

????public?function?bookAppointment(DateTime?$date,?$note)?{
????????$this->booked[]?=?array('date'?=>?$date->modify('midnight'),?'note'?=>?$note);
????}

????public?function?checkAvailability()?{
????????return?array_udiff($this->free,?$this->booked,?array($this,?'customCompare'));
????}
????
????public?function?customCompare($free,?$booked)?{
????????if?(is_array($free))?$a?=?$free['date'];
????????else?$a?=?$free;
????????if?(is_array($booked))?$b?=?$booked['date'];
????????else?$b?=?$booked;
????????if?($a?==?$b)?{
????????????return?0;
????????}?elseif?($a?>?$b)?{
????????????return?1;
????????}?else?{
????????????return?-1;
????????}
????}
}

//?Create?a?calendar?for?weekly?appointments
$myCalendar?=?new?MyCalendar;

//?Book?some?appointments?for?this?week
$myCalendar->bookAppointment(new?DateTime('Monday?this?week'),?"Cleaning?GoogleGuy's?apartment.");
$myCalendar->bookAppointment(new?DateTime('Wednesday?this?week'),?"Going?on?a?snowboarding?trip.");
$myCalendar->bookAppointment(new?DateTime('Friday?this?week'),?"Fixing?buggy?code.");

//?Check?availability?of?days?by?comparing?$booked?dates?against?$free?dates
echo?"I'm?available?on?the?following?days?this?week...\n\n";
foreach?($myCalendar->checkAvailability()?as?$free)?{
????echo?$free->format('l'),?"\n";?
}
echo?"\n\n";
echo?"I'm?busy?on?the?following?days?this?week...\n\n";
foreach?($myCalendar->booked?as?$booked)?{
????echo?$booked['date']->format('l'),?":?",?$booked['note'],?"\n";?
}
?>

上面的例子将输出:

代码语言:javascript
复制
本周我可以在以后几天...

星期二
星期四


这周我在接下来的几天里忙着...

星期一:清洁GoogleGuy的公寓。
星期三:继续滑雪之旅。
星期五:修复buggy代码。

笔记

注意:请注意,这个函数只检查n维数组的一个维度。当然,你可以使用array_udiff($ array10,$ array20,“data_compare_func”)来检查更深层次的维度

也可以看看

  • array_diff() - 计算数组的差异
  • array_diff_assoc() - 通过附加索引检查计算数组的差异
  • array_diff_uassoc() - 通过用户提供的回调函数执行附加索引检查来计算数组的差异
  • array_udiff_assoc() - 通过附加索引检查计算数组的差异,通过回调函数比较数据
  • array_udiff_uassoc() - 通过附加索引检查计算数组的差异,通过回调函数比较数据和索引
  • array_intersect() - 计算数组的交集
  • array_intersect_assoc() - 计算具有额外索引检查的数组的交集
  • array_uintersect() - 计算数组的交集,通过回调函数比较数据
  • array_uintersect_assoc() - 计算具有附加索引检查的数组的交集,通过回调函数比较数据
  • array_uintersect_uassoc() - 计算具有附加索引检查的数组交集,通过单独的回调函数比较数据和索引

← array_udiff_uassoc

array_uintersect_assoc →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com