检查数组中具有先前值的数组

时间:2014-02-19 18:42:43

标签: php

我有一个时间数组,阅读频率是5秒

我想检查

的每个元素之间的读取频率是否为5秒

阵列

我的代码

$freq_of_reading = 5;
$res_arr = array(
    0 => array("13:53:45"),
    1 => array("13:53:50"),
    2 => array("13:53:55"),
    3 => array("13:54:00"),
    4 => array("13:54:05"),
    5 => array("13:54:10"),
    6 => array("13:54:15"),
    7 => array("13:54:20"),
    8 => array("13:54:25"),
    9 => array("13:54:30"),
);


try {
    $u = 0;
    foreach ($res_arr as $key => $item) {
        // if(strtotime($item))
        //Logic here 
    }
} catch (Exception $e) {
    echo $exp = $e->getMessage();
}

3 个答案:

答案 0 :(得分:1)

试试这个循环:

for($i = 0; $i < count($res_arr) - 1; $i++)
{
    if(strtotime($res_arr[$i + 1][0]) - strtotime($res_arr[$i][0]) != $freq_of_reading)
    {
        echo "Element $i differs from the next one more than $freq_of_reading s";
        exit;
    }    
}

答案 1 :(得分:0)

试试这段代码

$count = count($res_arr);
for($i=0;$i<$count$i++)
    {
        $number = intval(substr($res_arr[$i], -2));

        $number1 = substr(($res_arr[$i+1], -2));
        if(($number1-$number) == 5)
        {
          echo "true";
        }

    }

答案 2 :(得分:0)

不确定为什么会出现在try中,但是:

$freq_of_reading = 5;
$previous = 0;

foreach($res_arr as $item) {
    $time = strtotime($item[0]);
    if($time - $freq_of_reading == $previous) {
        echo 'yes';
    }
    $previous = $time;
}