PHP多维数组差异

3

我有一些包含以下对象的数组,第一个数组:

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个结果:
  1. 第一个数组中时间比第二个数组更近的对象
  2. 第二个数组中时间比第一个数组更近的对象
  3. 第一个数组中'playertm'比第二个数组更近的对象
  4. 第二个数组中'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

感谢任何帮助。


我们可以假设数组按时间排序,就像您的示例一样吗?例如,第二个数组中最近的时间将是最后一个元素的时间。 - kba
为了帮助他人,每个数组的预期结果是什么? - Ja͢ck
[tm] 可以像示例一样按照从旧到新的顺序进行排序,但是 [playertm] 可能不行。期望的结果应该是:
  1. [1] [2]
  2. [5]
  3. [1] [2]
  4. [4] [5]
如果一个数组在 1 中出现,那么它就不应该出现在 2 中。同样的,如果一个数组在 3 中出现,那么它就不应该出现在 4 中。希望我已经解释得足够清楚了。相同的结果将被忽略,只有更近期的结果将从每个结果中返回。
- Thomas
2个回答

1
我不确定为什么会发生这种情况,但使用array_udiff()似乎很反直觉。我已经重写了两个函数来执行比较和迭代数组的要求:
function getCompareFunction($field)
{
        return function($a, $b) use ($field) {
                return strcmp($a->{$field}, $b->{$field});
        };
}

function getBigger($a, $b, $compare)
{
        $res = array();

        foreach ($a as $k => $v) {
                if (!isset($b[$k]) || $compare($v, $b[$k]) > 0) {
                        $res[$k] = $v;
                }
        }

        return $res;
}

$biggerTime = getCompareFunction('tm');
$biggerPlayerTime = getCompareFunction('playertm');

print_r(getBigger($a, $b, $biggerTime)); // [1, 2]
print_r(getBigger($b, $a, $biggerTime)); // [4, 5]
print_r(getBigger($a, $b, $biggerPlayerTime)); // [1, 2]
print_r(getBigger($b, $a, $biggerPlayerTime)); // [4, 5]

感谢您提供这个优美的解决方案。这是一种全新的思考方式,完全按照计划运作。我最初尝试了一些if语句/foreach循环,但并不知道strcmp函数。 - Thomas

0
希望您能将数组重构成以下结构,这样您就可以从两个对象中获得4种组合。随后您可以按照自己的要求进行排序。
[1] => stdClass Object
    (
        [matchID] => 1

        [tm] =>08:55:13
        [td] =>2014-01-16
        [playertm_date] => 2014-01-16
        [playertm_time] => 08:55:14

    )

另一种可选方案是Json


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接