我有一些包含以下对象的数组,第一个数组:
Array
(
[1] => stdClass Object
(
[matchID] => 1
[tm] => 2014-01-16 08:55:13
[playertm] => 2014-01-16 08:55:14
)
[2] => stdClass Object
(
[matchID] => 2
[tm] => 2014-01-16 09:53:50
[playertm] => 2014-01-16 09:53:52
)
[3] => stdClass Object
(
[matchID] => 3
[tm] => 2014-01-16 09:58:49
[playertm] => 2014-01-16 09:58:57
)
[4] => stdClass Object
(
[matchID] => 4
[tm] => 2014-01-17 08:44:34
[playertm] => 2014-01-17 08:44:35
)
)
第二个数组:
Array
(
[3] => stdClass Object
(
[matchID] => 3
[tm] => 2014-01-16 09:58:49
[playertm] => 2014-01-16 09:58:57
)
[4] => stdClass Object
(
[matchID] => 4
[tm] => 2014-01-17 08:44:34
[playertm] => 2014-01-17 08:44:38
)
[5] => stdClass Object
(
[matchID] => 5
[tm] => 2014-01-19 08:44:34
[playertm] => 2014-01-19 08:44:38
)
)
我正在尝试根据时间同步每个数组。我希望返回4个结果:
- 第一个数组中时间比第二个数组更近的对象
- 第二个数组中时间比第一个数组更近的对象
- 第一个数组中'playertm'比第二个数组更近的对象
- 第二个数组中'playertm'比第一个数组更近的对象
我正在使用'array_udiff'函数,目前有以下代码:
function tmCompare($a, $b)
{
return strtotime($a->tm) - strtotime($b->tm);
}
function ptmCompare($a, $b)
{
return strtotime($a->playertm) - strtotime($b->playertm);
}
$df1 = array_udiff($a, $b, 'tmCompare');
$df2 = array_udiff($b, $a, 'tmCompare');
$df3 = array_udiff($a, $b, 'ptmCompare');
$df4 = array_udiff($b, $a, 'ptmCompare');
这个功能似乎返回了差异,但是在最后两个函数中都会返回数组[4],而我只希望在时间更长而不仅仅是不同的情况下才返回。
我已经尝试过
return (strtotime($a->playertm) > strtotime($b->playertm)) ? -1 : 0;
我尝试了类似的方法,但好像无法得到正确的结果。我是错过了一些简单的东西还是方法不对呢?
编辑:这里有一个快速的pastebin链接,可以让代码运行 http://pastebin.com/gRz9v2kz
感谢任何帮助。