使用另一个日期数组从数组中查找调用数据的总和

时间:2014-05-31 05:32:24

标签: php

$array1 = Array (
    [0] => Array
        (
            [day] => 2014-05-07
            [total_Calls] => 1
        )

    [1] => Array
        (
            [day] => 2014-05-09
            [total_Calls] => 1
        )

    [2] => Array
        (
            [day] => 2014-05-12
            [total_Calls] => 1
        )

    [3] => Array
        (
            [day] => 2014-05-13
            [total_Calls] => 1
        )

    [4] => Array
        (
            [day] => 2014-05-14
            [total_Calls] => 2
        )
    [5] => Array
        (
            [day] => 2014-05-16
            [total_Calls] => 4
        )

);


$array2 = Array (
    [0] => Array
        (
            [report_date] => 1397413800-1397932200
        )

    [1] => Array
        (
            [report_date] => 1398018600-1398537000
        )

    [2] => Array
        (
            [report_date] => 1398623400-1399141800
        )

    [3] => Array
        (
            [report_date] => 1399228200-1399746600
        )

    [4] => Array
        (
            [report_date] => 1399833000-1400351400
        )

    [5] => Array
        (
            [report_date] => 1400437800-1400956200
        )

)

我想找到第二个数组中日期之间的total_Calls总和。我需要这样做。

我有输出使用for循环但我不想用于循环

 $array = array();
     foreach ($array1 as $val) {
         $temp = str_replace('-', '', $val['day']);
         $array[strtotime($temp)] = $val['total_Calls'];
     }
      $week = array();

     $sum = array();
     foreach ($array as $k => $v) {
         foreach ($array2 as $val) {
             $temp = explode('-', $val['report_date']);
             if ($k >= $temp[0] && $k <= $temp[1]) {
                 $sum[$val['report_date']][] = $v;
             } else {
                 $sum[$val['report_date']][] = 0;
             }
         }
     }

输出:

     [1397413800-1397932200] => 0
     [1398018600-1398537000] => 0
     [1398623400-1399141800] => 0
     [1399228200-1399746600] => 2
     [1399833000-1400351400] => 9
     [1400437800-1400956200] => 8

2 个答案:

答案 0 :(得分:0)

暴力破解!!!

<?php
$from_to = array();
foreach ($array2 as $data) {
    $days = explode('-', $data['report_date']);
    $from_to[] = $days;
}

$counter = array();
foreach ($array1 as $data) {
    $day = new Datetime($data['day'], new Datetimezone('UTC'));
    $timestamp = $day->('U');
    foreach ($from_to as $range) {
        if ($timestamp >= $range[0] && $timestamp <= $range[1]) {
            $key = implode('-', $range);
            if (!isset($counter[$key])) {
                $counter[$key] = 0;
            }

            $counter[$key] += $data['total_Calls'];
        }
    }
}

print_r($counter);

答案 1 :(得分:0)

试图避免使用循环是没有意义的。事实上,无论如何,你将使用循环。通过编写一个,或使用一些最终将使用一个的本机或自定义函数。因为这只是你走过数组的方式。

然而,您可能想要做的是尝试尽可能少的循环。并尽可能做繁重的工作,例如strtotime()外部(大部分)循环。

我已经让你的代码走了过来并将其压缩。您将在$new_array内找到结果。

foreach($array1 as $key=>$value)
    $array1[$key]["timestamp"] = strtotime($array1[$key]["day"]);
foreach($array2 as $report) {
    $new_array[$report["report_date"]] = 0;
    $from_to = array_map("intval",explode("-",$report["report_date"]));
    foreach($array1 as $call_day) {
        $day = $call_day["timestamp"];
        if ($day >= $from_to[0] &&  $day < $from_to[1])
            $new_array[$report["report_date"]] += $call_day["total_Calls"];
    }
}

DEMO