我有以下数组:
Array
(
[0] => Array
(
[serial_number] => 00127B4EE68F
[start_log_date] => 00:34:28
[log_date] => 00:36:52
[good_files] => 11
[bad_files] => 0
[interrupted_files] => 0
[skipped_door_files] => 0
[max_rate] => 560.52
[min_rate] => 215.59
[avg_rate] => 360.54
[total_bytes_before_transfer] => 40598530
[total_bytes_after_transfer] => 0
[firmware] => 10.6
)
[1] => Array
(
[serial_number] => 00127B4F0F98
[start_log_date] => 00:59:55
[log_date] => 01:00:21
[good_files] => 1
[bad_files] => 0
[interrupted_files] => 0
[skipped_door_files] => 0
[max_rate] =>
[min_rate] =>
[avg_rate] =>
[total_bytes_before_transfer] => 2276
[total_bytes_after_transfer] => 0
[firmware] => 10.6
)
[2] => Array
(
[serial_number] => 00127B4F0F97
[start_log_date] => 01:00:59
[log_date] => 01:01:55
[good_files] => 4
[bad_files] => 0
[interrupted_files] => 0
[skipped_door_files] => 0
[max_rate] => 557.7
[min_rate] => 334.16
[avg_rate] => 397.21
[total_bytes_before_transfer] => 11979855
[total_bytes_after_transfer] => 0
[firmware] => 10.6
)
[3] => Array
(
[serial_number] => 00127B4ED82D
[start_log_date] => 01:08:10
[log_date] => 01:08:35
[good_files] => 1
[bad_files] => 0
[interrupted_files] => 0
[skipped_door_files] => 0
[max_rate] =>
[min_rate] =>
[avg_rate] =>
[total_bytes_before_transfer] => 234
[total_bytes_after_transfer] => 0
[firmware] => 10.6
)
[4] => Array
(
[serial_number] => 00127B4EE61D
[start_log_date] => 01:54:51
[log_date] => 01:55:17
[good_files] => 1
[bad_files] => 0
[interrupted_files] => 0
[skipped_door_files] => 0
[max_rate] =>
[min_rate] =>
[avg_rate] =>
[total_bytes_before_transfer] => 68136
[total_bytes_after_transfer] => 0
[firmware] => 10.6
)
)
我想检查log_date
的值是否小于下一个start_log_date
。如果它更小,请计算并继续。我做了一个foreach
循环,但我没有得到我想要的结果。
这就是我想要显示结果的方式(例子):
Simultaneous connections: 2 - from 03:38:55 PM to 04:51:10 PM - ( 1h 12 min )
1 serial number start_log_date log_date
2 serial number start_log_date log_date
Simultaneous connections: 1 - from 03:38:55 PM to 03:40:10 PM - ( 1 min )
1 serial number start log date log_date
etc...
这是我的初始循环:
$iCount = 1;
$i = 0;
foreach($aRecordsPerDay as $key=>$aRow)
{
$aTimeStart[] = strtotime($aRow["start_log_date"]);
$aTimeStopOld[] = strtotime($sTimeStopOld);
if(strtotime($aRow['start_log_date']) < max($aTimeStopOld))
{
$sTimeStart = $aTimeStart[$i-$iCount];
$sTimeStop = $aRow['log_date'];
if(strtotime($sTimeStop) < strtotime($sTimeStopOld))
$sTimeStop = $sTimeStopOld;
$aTimeStop[] = strtotime($sTimeStop);
$iCount+=1;
}
else
{
if($iCount > 1)
{
echo "Simultaneous connections".$iCount;
$iCount = 1;
}
else
{
echo "Simultaneous connections 1";
$iCount = 1;
}
}
$sTimeStartOld=$aRow['start_log_date'];
$sTimeStopOld=$aRow['log_date'];
$i++;
///show data
}
这是我得到的结果:
1 serial number start_log_date log_date
2 serial number start_log_date log_date
Simultaneous connections: 2 - from 03:38:55 PM to 04:51:10 PM - ( 1h 12 min )
1 serial number start log date log_date
Simultaneous connections: 1 - from 03:38:55 PM to 03:40:10 PM - ( 1 min )
etc..
答案 0 :(得分:3)
您可以对for
个对象使用DateTime
循环:
$count = 0; // initialize counter
for ($i=0; $i < count($array) - 1 ; $i++) {
$log_date = new DateTime($array[$i]['log_date']);
$start_log_date = new DateTime($array[$i+1]['start_log_date']);
if ($log_date < $start_log_date) {
$count++;
} else {
break;
}
}
输出:
4
答案 1 :(得分:0)
如果您使用strtotime(),则应该有效。
$count = 0;
for ($i = 0; $i < count($array) - 1; ++$i) {
if (strtotime($array[$i]['log_date']) < strtotime($array[$i + 1]['log_date'])) ++$count;
}
答案 2 :(得分:-3)
您可以在不使用for循环的情况下访问数组的每个项目。
for($i = 0; $i < $ArrayName.length; $++) {
if($ArrayName[$i]['log_date'] < $ArrayName[$i+1]['start_log_date']) {
$count++;
}
}