在最后一小时内循环遍历数组结果

时间:2017-01-22 14:39:31

标签: php

我有一个查询API并在最后一天查找记录的脚本。

我想循环完成这些结果,并在过去一小时内完成这些结果。

有人能解释我是怎么做到的吗?

这是我的每日结果:

array(2) {
  [0]=>
  array(36) {
    ["CallRef"]=> string(10) "1234567891"
    ["CallStartTime"]=> string(8) "08:18:30"
  }
  [1]=>
  array(36) {
    ["CallRef"]=> string(10) "1234567892"
    ["CallStartTime"]=> string(8) "14:04:20"
  }
}

这是英国的14:40所以我的脚本应该从阵列中抓取第二项。

5 个答案:

答案 0 :(得分:1)

这个怎么样?

$apiElements = [
    ['CallRef' => '1234567891', 'CallStartTime' => '08:18:30'],
    ['CallRef' => '1234567892', 'CallStartTime' => '14:04:20'],
];

$currentFormatted = (new DateTime())->format('H');
$startOfHour = DateTime::createFromFormat('H:i:s', $currentFormatted . ':00:00');
$endOfHour = DateTime::createFromFormat('H:i:s', $currentFormatted . ':59:59');

$callsInHour = array_filter($apiElements, function($element) use ($startOfHour, $endOfHour) {
    $dt = DateTime::createFromFormat('H:i:s', $element['CallStartTime']);
    return $dt >= $startOfHour && $dt <= $endOfHour; 
});

完全未经测试,但试一试。

下次,请发布您尝试过的代码......

答案 1 :(得分:1)

有多种方法可以做到这一点。您可以从一小时前获得一个Unix时间映射:strtotime('-1 hour')。您可以在:的{​​{1}}时间内explode获得时间,并从爆炸结果中获取第二个结果。

foreach ($array as $value){
    $exploded=explode(":",$value['CallStartTime']);
    if(date('H')-1>$exploded[1]){
       //This means it's an hour ago.
    }
}

答案 2 :(得分:0)

替换lapply并尝试:

> lapply(rfs, as.data.frame(predict))[1]
[[1]]
                       value
Merc 230            22.85464
Merc 450SE          17.61810
Fiat 128            22.31571
Porsche 914-2       23.95909
Valiant             21.28786
Pontiac Firebird    15.93824
Ford Pantera L      21.20373
Chrysler Imperial   14.40740
Lincoln Continental 16.43074
Mazda RX4 Wag       21.18467

答案 3 :(得分:0)

更灵活的解决方案:

首先,您需要一个将时间转换为秒的功能

function convertTimeToSeconds($hhmmss)
{
    //correct format if needed
    list($hours, $minutes, $seconds) = explode(':', $hhmmss);
    return $hours * 3600 + $minutes * 60 + $seconds;
}

然后你就可以使用array_filter功能

    $now = date('H:i:s');
    $nowSeconds = convertTimeToSeconds($now);
    $filterFunction = function ($value) use ($nowSeconds) {
        return ($nowSeconds - convertTimeToSeconds($value['CallStartTime'])) < 3600; //3600 seconds = hour
    };
    $filteredList = array_filter($array, $filterFunction);

全部https://3v4l.org/icAmp

答案 4 :(得分:0)

我想你有array

    Array
(
    [0] => Array
        (
            [CallRef] => 1234567892
            [CallStartTime] => 21:04:20
        )

    [1] => Array
        (
            [CallRef] => 1234567892
            [CallStartTime] => 09:08:08
        )

    [2] => Array
        (
            [CallRef] => 1234567892
            [CallStartTime] => 08:11:08
        )

    [3] => Array
        (
            [CallRef] => 1234567892
            [CallStartTime] => 20:59:08
        )

)

所以我试过这个

    <?php

     $arr = array( 
            "0" => array (
               "CallRef" => 1234567892,
               "CallStartTime" => "21:04:20"
            ),

            "1" => array (
                "CallRef" => 1234567892,
               "CallStartTime" => "09:08:08"
            ),
            "2" => array (
                "CallRef" => 1234567892,
               "CallStartTime" => "08:11:08"
            ),
            "3" => array (
                "CallRef" => 1234567892,
               "CallStartTime" => "20:59:08"
            )
         );
         //echo "<pre>";print_r($arr);die;
     date_default_timezone_set('Asia/kolkata');//set your timezone
     $currentTime = date('H:i:s');//getting current time
     $lastHourTime = date('H:i:s', strtotime('-1 hour'));//getting last hour time
     $result = array();
     foreach ($arr as $singlearr){
         if(strtotime($singlearr['CallStartTime']) >= strtotime($lastHourTime) && strtotime($singlearr['CallStartTime']) <= strtotime($currentTime)){
             $result[] = $singlearr;    
         }else{

         }

}
     echo "<pre>";print_r($result);die;//this gives me last hour records only

Check Demo当您检查演示时,所有记录time > last hour将返回,否则将返回空数组

可能会有所帮助!