如何正确使用array_udiff()? [英] How do I use array_udiff() correctly?

查看:60
本文介绍了如何正确使用array_udiff()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个多维数组,它们看起来都像这样:

I have two multidimensional arrays that both look something like this:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 00:00:00
    ),

    [1] => Array (
         'id' => 6,
         'other' => 'another string',
         'timestamp' => 1835-01-01 00:00:00
    )
)

我试图找到一种方法来找出哪些元素显示在一个数组中( $ b ),但是没有另一个( $ a )以及是否有任何值更改的元素。如果 $ a 是:

I'm trying to find a way to figure out which elements show up in one array ($b), but not the other ($a) and if there are any elements with changed values. If $a is:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 00:00:00
    )
)

$ b 是:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 12:12:12
    ),

    [1] => Array (
         'id' => 4,
         'other' => 'some string',
         'timestamp' => 1900-01-01 01:12:23
    )
)

然后函数应返回:

Array
(
    [0] => Array (
         'id' => 3,
         'other' => 'some string',
         'timestamp' => 2000-01-01 12:12:12
    ),

    [1] => Array (
         'id' => 4,
         'other' => 'some string',
         'timestamp' => 1900-01-01 01:12:23
    )
)

因为 id = 3 的元素已更改( timestamp 字段),而的元素已更改id = 4 是新的,不会出现在其他数组中。

because the element with id = 3 has been changed (the timestamp field) and the element with id = 4 is new and doesn't appear in the other array.

我一直在尝试使用 array_udiff ,但我仍然不知道它是如何工作的(它似乎先对两个数组进行排序,但是然后如何进行比较?)。 array_udiff 是正确的方法还是应该编写自定义函数?

I've been trying to do this with array_udiff, but I still don't know how it works (it seems to sort both arrays first, but then how does it do comparison?). Is array_udiff the proper method or should I write a custom function?

推荐答案

您可以使用 array_udiff 并定义自己的比较回调。我假设这两个数组的结构完全相同。

You can use array_udiff and define your own comparison callback. I assume that both arrays has exactly the same structure.

您可以定义自己的回调函数,如下所示:

You can define your own callback function as follow:

int comparison(Array $a, Array $b){
    if ($a['id']==$b['id'] && $a['other']==$b['other'] && $a['timestamp']==$b['timestamp']){
        return 0
    }else{
        return -1
    }
}

回调函数必须返回如果第一个参数小于第二个参数,则为负整数;正数(如果更大);或0(等于)。然后,您可以返回不等于0的任何数字,以指示参数不同,如果相等则返回0。

The callback function must return a negative integer if first argument is less than the second; a positive number if it's bigger; or 0 if it's equal. Then, you can return any number different to 0 to indicate that the arguments are different and 0 if they are the equal.

最后,应调用 array_udiff 如下:

array_udiff($a, $b, 'comparison')

您将获得 $ a $ b 中没有或不同的。

And you will get a list of the elements of $a which are not, or are different in $b.

请注意,如果要比较2个数组当其中一个元素比另一个元素多时,应将包含新元素的数组作为第一个参数传递。

Note that if you want to compare 2 array when one of then has more elements than the other, you should pass as first argument the array with the new elements.

这篇关于如何正确使用array_udiff()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆