如何根据当前/上一个数组中的值将数据合并到多维数组中

时间:2014-12-06 05:10:38

标签: php mysql arrays multidimensional-array merge

我正在构建一些手机统计信息并且输出低于我需要基本合并相同的src并将持续时间加在一起。例如,数组索引0到6将合并为一个如下所示的数组:

[0] => Array ( [calldate] => 2014-12-01 08:31:47 [src] => 1234 [duration] => 190 )

您可能认为它不应该是索引0到8,但因为索引6只有10秒,这基本上表示呼叫已被应答。可以假设,如果呼叫持续时间为30秒,则表示尚未应答。

这并不像在数组中找到匹配的src并将所有持续时间加在一起一样简单,但您会在index 11 9123次来电中看到,并在12秒内得到解答但他们大约30分钟后回电话,他们在9:55:31的第二个电话会在15秒内回复,这需要将最终阵列中的项目分开。

src只是虚构的,不会是现实世界中的数字。

Array ( 

    [0] => Array ( [calldate] => 2014-12-01 08:31:47 [src] => 1234 [duration] => 30 ) 
    [1] => Array ( [calldate] => 2014-12-01 08:32:22 [src] => 1234 [duration] => 30 ) 
    [2] => Array ( [calldate] => 2014-12-01 08:32:57 [src] => 1234 [duration] => 30 ) 
    [3] => Array ( [calldate] => 2014-12-01 08:33:32 [src] => 1234 [duration] => 30 ) 
    [4] => Array ( [calldate] => 2014-12-01 08:34:07 [src] => 1234 [duration] => 30 ) 
    [5] => Array ( [calldate] => 2014-12-01 08:34:42 [src] => 1234 [duration] => 30 ) 
    [6] => Array ( [calldate] => 2014-12-01 08:35:12 [src] => 1234 [duration] => 10 ) 
    [7] => Array ( [calldate] => 2014-12-01 08:35:47 [src] => 1234 [duration] => 30 ) 
    [8] => Array ( [calldate] => 2014-12-01 08:36:22 [src] => 1234 [duration] => 30 ) 
    [9] => Array ( [calldate] => 2014-12-01 08:51:20 [src] => 5678 [duration] => 15 ) 
    [10] => Array ( [calldate] => 2014-12-01 09:09:14 [src] => 5678 [duration] => 10 ) 
    [11] => Array ( [calldate] => 2014-12-01 09:22:58 [src] => 9123 [duration] => 12 ) 
    [12] => Array ( [calldate] => 2014-12-01 09:42:42 [src] => 4567 [duration] => 30 ) 
    [13] => Array ( [calldate] => 2014-12-01 09:43:17 [src] => 4567 [duration] => 30 ) 
    [14] => Array ( [calldate] => 2014-12-01 09:43:52 [src] => 4567 [duration] => 30 ) 
    [15] => Array ( [calldate] => 2014-12-01 09:44:27 [src] => 4567 [duration] => 30 ) 
    [16] => Array ( [calldate] => 2014-12-01 09:45:02 [src] => 4567 [duration] => 29 ) 
    [17] => Array ( [calldate] => 2014-12-01 09:55:31 [src] => 9123 [duration] => 15 ) 
    [18] => Array ( [calldate] => 2014-12-01 10:34:29 [src] => 8912 [duration] => 10 ) 
    [19] => Array ( [calldate] => 2014-12-01 13:26:29 [src] => 3456 [duration] => 30 ) 
    [20] => Array ( [calldate] => 2014-12-01 13:27:04 [src] => 3456 [duration] => 30 ) 
    [21] => Array ( [calldate] => 2014-12-01 13:27:39 [src] => 3456 [duration] => 30 ) 
    [22] => Array ( [calldate] => 2014-12-01 13:28:14 [src] => 3456 [duration] => 30 ) 
    [23] => Array ( [calldate] => 2014-12-01 13:28:49 [src] => 3456 [duration] => 30 ) 
    [24] => Array ( [calldate] => 2014-12-01 13:29:24 [src] => 3456 [duration] => 30 ) 
    [25] => Array ( [calldate] => 2014-12-01 13:29:54 [src] => 3456 [duration] => 4 ) 
)

我认为这需要某种for循环,它必须比较当前/先前的调用并将它们添加到新数组中。我已经这样做但它没有给出预期的结果,因为它只是在src匹配时将前一个和当前的调用加在一起。这是我目前的代码:

$adjusted = array();
for($i = 1; $i < sizeof($adjustedTotalCallsExt); $i++) {
$previousCall = $adjustedTotalCallsExt[$i-1];
$currentCall = $adjustedTotalCallsExt[$i];
    if($previousCall['src'] == $currentCall['src']) {
        $adjusted[] = $previousCall['duration'] + $currentCall['duration'];
    }
    else {
    $adjusted[] = $currentCall['duration'];
    }
}     

2 个答案:

答案 0 :(得分:0)

$adjusted = array();
        for($i = 1; $i < sizeof($adjustedTotalCallsExt); $i++) 
        {       
                $adjusted[$adjustedTotalCallsExt[$i]['src']]['duration']=$adjusted[$adjustedTotalCallsExt[$i]['src']]['duration']+ $adjustedTotalCallsExt[$i]['duration'] ;         
        } 

希望它能满足您的需求。如果它不适合你,请告诉我。

答案 1 :(得分:0)

如果您尝试根据单独实例中的已解答来分离每个呼叫, PLUS 您希望根据每个实例对同名src进行不同的计数,这会使您的阵列失效基于时间跨度的部分(好吧,不是实时,只是数组键的差异)。请注意,如果需要,您可以再次将其分解为仅隔离每个总计实例:

$calls = array(
                0 => array('calldate' => '2014-12-01 08:31:47', 'src' => '1234', 'duration' => 30 ),
                1 => array('calldate' => '2014-12-01 08:32:22', 'src' => '1234', 'duration' => 30 ),
                2 => array('calldate' => '2014-12-01 08:32:57', 'src' => '1234', 'duration' => 30 ), 
                3 => array('calldate' => '2014-12-01 08:33:32', 'src' => '1234', 'duration' => 30 ), 
                4 => array('calldate' => '2014-12-01 08:34:07', 'src' => '1234', 'duration' => 30 ), 
                5 => array('calldate' => '2014-12-01 08:34:42', 'src' => '1234', 'duration' => 30 ), 
                6 => array('calldate' => '2014-12-01 08:35:12', 'src' => '1234', 'duration' => 10 ), 
                7 => array('calldate' => '2014-12-01 08:35:47', 'src' => '1234', 'duration' => 30 ), 
                8 => array('calldate' => '2014-12-01 08:36:22', 'src' => '1234', 'duration' => 30 ), 
                9 => array('calldate' => '2014-12-01 08:51:20', 'src' => '5678', 'duration' => 15 ), 
                10 => array('calldate' => '2014-12-01 09:09:14', 'src' => '5678', 'duration' => 10 ), 
                11 => array('calldate' => '2014-12-01 09:22:58', 'src' => '9123', 'duration' => 12 ), 
                12 => array('calldate' => '2014-12-01 09:42:42', 'src' => '4567', 'duration' => 30 ), 
                13 => array('calldate' => '2014-12-01 09:43:17', 'src' => '4567', 'duration' => 30 ), 
                14 => array('calldate' => '2014-12-01 09:43:52', 'src' => '4567', 'duration' => 30 ), 
                15 => array('calldate' => '2014-12-01 09:44:27', 'src' => '4567', 'duration' => 30 ), 
                16 => array('calldate' => '2014-12-01 09:45:02', 'src' => '4567', 'duration' => 29 ), 
                17 => array('calldate' => '2014-12-01 09:55:31', 'src' => '9123', 'duration' => 15 ), 
                18 => array('calldate' => '2014-12-01 10:34:29', 'src' => '8912', 'duration' => 10 ), 
                19 => array('calldate' => '2014-12-01 13:26:29', 'src' => '3456', 'duration' => 30 ), 
                20 => array('calldate' => '2014-12-01 13:27:04', 'src' => '3456', 'duration' => 30 ), 
                21 => array('calldate' => '2014-12-01 13:27:39', 'src' => '3456', 'duration' => 30 ), 
                22 => array('calldate' => '2014-12-01 13:28:14', 'src' => '3456', 'duration' => 30 ), 
                23 => array('calldate' => '2014-12-01 13:28:49', 'src' => '3456', 'duration' => 30 ), 
                24 => array('calldate' => '2014-12-01 13:29:24', 'src' => '3456', 'duration' => 30 ), 
                25 => array('calldate' => '2014-12-01 13:29:54', 'src' => '3456', 'duration' => 4 ),
                26 => array('calldate' => '2014-12-01 13:29:54', 'src' => '1234', 'duration' => 30 )
            );
$i = 0;
foreach($calls as $row) {

        $user[$row['src']][$i]['calls'][]   =   $row['duration'];
        $user[$row['src']][$i]['duration']  =   array_sum($user[$row['src']][$i]['calls']);

        if($row['duration'] < 30) {
                $i++;
            }
    }

echo '<pre>';
print_r($user);

会给你:

Array
(
    [1234] => Array
        (
            [0] => Array
                (
                    [calls] => Array
                        (
                            [0] => 30
                            [1] => 30
                            [2] => 30
                            [3] => 30
                            [4] => 30
                            [5] => 30
                            [6] => 10
                        )

                    [duration] => 190
                )

            [1] => Array
                (
                    [calls] => Array
                        (
                            [0] => 30
                            [1] => 30
                        )

                    [duration] => 60
                )

            [5] => Array
                (
                    [calls] => Array
                        (
                            [0] => 30
                        )

                    [duration] => 30
                )

        )

    [5678] => Array
        (
            [1] => Array
                (
                    [calls] => Array
                        (
                            [0] => 15
                            [1] => 10
                        )

                    [duration] => 25
                )

        )

    [9123] => Array
        (
            [2] => Array
                (
                    [calls] => Array
                        (
                            [0] => 12
                        )

                    [duration] => 12
                )

            [3] => Array
                (
                    [calls] => Array
                        (
                            [0] => 15
                        )

                    [duration] => 15
                )

        )

    [4567] => Array
        (
            [3] => Array
                (
                    [calls] => Array
                        (
                            [0] => 30
                            [1] => 30
                            [2] => 30
                            [3] => 30
                            [4] => 29
                        )

                    [duration] => 149
                )

        )

    [8912] => Array
        (
            [3] => Array
                (
                    [calls] => Array
                        (
                            [0] => 10
                        )

                    [duration] => 10
                )

        )

    [3456] => Array
        (
            [4] => Array
                (
                    [calls] => Array
                        (
                            [0] => 30
                            [1] => 30
                            [2] => 30
                            [3] => 30
                            [4] => 30
                            [5] => 30
                            [6] => 4
                        )

                    [duration] => 184
                )

        )

)