我正在编写估算时间的函数。 i-e根据时间运行特定事件...假设我有一个数组时间
$times = array(
'00:00' => 'Programme 1',
'00:30' => 'Programme 2',
'01:00' => 'Programme 3',
'01:30' => 'Programme 4',
'02:00' => 'Programme 5',
'02:30' => 'Programme 6',
'03:00' => 'Programme 7',
'03:30' => 'Programme 8',
'04:00' => 'Programme 9',
'04:30' => 'Programme 10',
'05:00' => 'Programme 10',
);
功能
echo (currently_should_play("00:05", $times) === 'Programme 1' ? 'Ok 1' : 'Not Ok 1') . PHP_EOL;
如果第一次小于第二次它应该运行第一次并且如果它等于精确值而不是它的运行那么它应匹配...
上方函数调用应与00:00匹配并打印' ok'
让我们假设另一个例子
echo (currently_should_play("00:58", $time) === 'Programme 2' ? 'Ok 2' : 'Not Ok 2') . PHP_EOL;
它应与第二个值匹配,因为它与程序2 ...
匹配请提出一些解决方案..
我的功能代码在这里,我从数组
获取键和值function currently_should_play($current_time, array $times) {
foreach ($times as $time => $programme) {
echo $time;
echo $programme."<br/>";
}
}
答案 0 :(得分:0)
function currently_should_play($current_time, array $times) {
$s=size($times);
while($s-->0){
if($times[$s]==$current_time){
return $times[$s];
}elseif($times[$s-1]==$current_time){
return $times[$s-1];
}elseif($times[$s-1]<$current_time&&$times[$s]>$current_time){
return $times[$s-1];
}
}
return false;
}