在循环

时间:2015-07-24 13:31:04

标签: php arrays loops multidimensional-array

假设我将代理存储在具有以下结构的多维数组中:

[Agent - 167] => Array
        (
            [0] => Array
                (
                    [0] => 2015-07-17 00:01:51.417080
                    [1] => Agent - 167
                    [2] => LOGIN
                )

            [1] => Array
                (
                    [0] => 2015-07-17 00:02:28.821206
                    [1] => Agent - 167
                    [2] => LOGOUT
                )

            [2] => Array
                (
                    [0] => 2015-07-17 00:02:37.257944
                    [1] => Agent - 167
                    [2] => LOGIN
                )

            [3] => Array
                (
                    [0] => 2015-07-21 07:16:51.457435
                    [1] => Agent - 167
                    [2] => LOGIN
                )
       [4] => Array
                (
                    [0] => 2015-07-21 07:20:51.016638
                    [1] => Agent - 167
                    [2] => LOGOUT
                )
    )

我想计算每个代理的每个LoginLogout事件之间的时差,并获得汇总。这很简单,因为时间日志位于内部数组的第一个元素中[0][0] = Time。该事件是第三个元素。我的计算仅在以下事件为LOGOUT且前一事件为LOGIN时才有效。

但是,有时代理会超时而不是注销连续记录两个LOGIN事件,我想跳过具有LOGIN事件 IF 的数组next数组也有一个LOGIN事件并继续我的计算。

我使用以下代码执行计算:

  foreach ($pse_array as $value) {
        $total = 0;
        for ($i = 0; $i < count($value); $i+=2) {
            $srtTime = strtotime($value[$i][0]);
            $endTime = strtotime($value[$i + 1][0]);
            $interval = $endTime - $srtTime;
            $total += $interval;
        }
        echo gmdate("H:i:s", $total) . " Minutes <br>";
    }

我也知道我可以进行比较以检查事件是否符合If statement的标准,但我不确定如何跳过该数组。

2 个答案:

答案 0 :(得分:0)

$timeout = strtotime('00:30') - strtotime('00:00');

foreach ($pse_array as $value) { // loop by user
    $total = 0;
    $login = false;                          // time of login  
    foreach ($value as $item)  {             // loop by entry
        if ($item[2] == 'LOGIN') {
           //if ($login) $total += $timeout; // two login successively
                                             // you can remove this
            $login = $item[0];               // save login time
            continue;
        }
        $srtTime = strtotime($login);        // if here, status = LOGOUT
        $login = false;                      // mark that previos was logout 
        $endTime = strtotime($item[0]);      // further your code
        $interval = $endTime - $srtTime;
        $total += $interval;                 // You receive interval in sec
    }
    echo $total/60 . " Minutes <br>";       
}

答案 1 :(得分:0)

在几次尝试后管理以找到快速简便的解决方案

$pse_array = [ 'Agent - 167' => [
        [ 0 => '2015-07-17 00:01:51.417080',
            1 => 'Agent - 167',
            2 => 'LOGIN'
        ],
        [
            0 => '2015-07-17 00:02:28.821206',
            1 => 'Agent - 167',
            2 => 'LOGOUT'
        ],
        [
            0 => '2015-07-17 00:02:37.257944',
            1 => 'Agent - 167',
            2 => 'LOGIN'
        ],
        [
            0 => '2015-07-21 07:16:51.457435',
            1 => 'Agent - 167',
            2 => 'LOGIN'
        ],
        [
            0 => '2015-07-21 07:20:51.016638',
            1 => 'Agent - 167',
            2 => 'LOGOUT'
        ]
    ]
];


foreach ($pse_array as $value) {
    $total = 0;
    for ($i = 0; $i < count($value);
    ) /* $i+=2) */ {
        if ($value[$i][2] === "LOGIN" && $value[$i + 1][2] === "LOGIN") {
            $i+=1;
        } else {

            $srtTime = strtotime($value[$i][0]);
            $endTime = strtotime($value[$i + 1][0]);
            $interval = $endTime - $srtTime;
            $total += $interval;
            $i+=2;
        }
    }
    echo gmdate("H:i:s", $total) . " Minutes <br>";
}

工作箱:https://eval.in/406539