douchun1961 2014-01-19 17:49
浏览 48
已采纳

PHP多阵列差异

I have arrays containing objects that look like the following, first array:

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
    )
)

Second array:

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
    )
)

And am trying to synchronize each array based on the times. I want 4 results to be returned:

  1. The objects in the first array that have a time more recent than the second array
  2. The objects in the second array that have a time more recent than the first array
  3. The objects in the first array that have a 'playertm' more recent than the second array
  4. The objects in the second array that have a 'playertm' more recent than the first array

Some results may not be in each array and will need to be returned, however the array keys will always match.

I am using the 'array_udiff' function and so far have the following:

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');

Which appears to return the differences, however array [4] is returned in each of the last 2 functions, whereas I only want it to be returned if the time is larger rather than only different.

I have tried

return (strtotime($a->playertm) > strtotime($b->playertm)) ? -1 : 0;

and similar but cannot seem to get the correct results. Am I missing something simple here or going about this wrong?

Edit: here is a quick pastebin to get the code running http://pastebin.com/gRz9v2kz

Thanks for any help.

  • 写回答

2条回答 默认 最新

  • ds3464 2014-01-20 14:58
    关注

    I'm not sure why this happens, but the use of array_udiff() seems counterintuitive. I've rewritten the requirements in two functions to perform the comparison and iterate the array:

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化