我有一个数组:
array(2) {
["Status"]=>
string(3) "001"
["Data"]=>
array(1) {
["item"]=>
array(87) {
[0]=>
array(36) {
["StartDate"]=>
string(10) "2017-01-13"
["StartTime"]=>
string(8) "07:02:18"
}
}
}
}
如果StartTime
在最后30秒内,我希望基本上从中选择数据 - 根据服务器时间。
这样做的最佳方式是什么?
答案 0 :(得分:3)
您可以使用array_filter()
功能:
$out = array_filter(
$values, // your original array
function($element)
{
$current_time = time();
// change the keys to match your input
$element_time = strtotime($element['start_date'] .
' ' .
$element['start_time']);
// fix this condition according to your needs
return $current_time - $element_time < 30;
}
);
// now $out contains filtered values and $values is unchanged
答案 1 :(得分:0)
function getFilteredCallsByDate($calls, $since) {
return new CallbackFilterIterator(new ArrayIterator($calls['Data']['item']), function ($call) use ($since) {
return strtotime(sprintf('%s %s', $call['StartDate'], $call['StartTime'])) >= strtotime($since);
});
}
$calls = [
'Data' => [
'item' => [
['StartDate' => '2017-01-15', 'StartTime' => '07:38:18'],
['StartDate' => '2017-01-15', 'StartTime' => '08:38:18'],
['StartDate' => '2017-01-15', 'StartTime' => '11:25:59'],
]
]
];
var_dump($calls);
echo "The time is " . date("h:i:sa");
foreach (getFilteredCallsByDate($calls, '-30 seconds') as $call) {
var_dump($call);
}