我需要帮助逻辑来获得min&每个id的数组中的最大值取决于日期。
这是数组的例子:
$list_data = array(
array('20', '2016-08-04', '10:07:42'),
array('20', '2016-08-04', '18:07:34'),
array('35', '2016-08-05', '10:09:39'),
array('35', '2016-08-05', '10:09:45'),
array('35', '2016-08-05', '18:03:18'),
array('35', '2016-08-05', '18:03:18'),
array('39', '2016-08-05', '10:09:23'),
array('39', '2016-08-05', '10:09:25'),
array('39', '2016-08-05', '18:03:36'),
array('39', '2016-08-05', '18:03:37'),
array('35', '2016-08-08', '10:09:39'),
array('35', '2016-08-08', '10:09:45'),
array('35', '2016-08-08', '18:03:18'),
array('35', '2016-08-08', '18:03:18'),
array('39', '2016-08-08', '10:09:23'),
array('39', '2016-08-08', '10:09:25'),
array('39', '2016-08-08', '18:03:36'),
array('39', '2016-08-08', '18:03:37'),
);
index[0] = ID,
index[1] = Date,
index[2] = Time,
我如何打印数组
foreach($list_data as $data){
echo $data[0];
echo '<br>';
echo $data[1];
echo '<br>';
echo $data[2];
echo '<br>';
echo '<br>';
}
现在,我想要的是在每smallest time
biggest time
找到ID
和Date
我要显示的示例列表:
结论: 阵列的引发列表数据来自另一台机器(格式日期和时间不能更改为unixtimestamp),所以这就是我避免使用&#39; strtotime&#39;。
结果:
转换日期的功能,因此我没有使用&#39; strtotime&#39;功能
function date_convert_to_time($date){
$new_date = new DateTime($date);
return $new_date->format('U');
}
第一组数组
$arr = array();
// group them
foreach ($list_data as $v) {
// first by id, then by date
$arr[$v[0]][$v[1]][] = date_convert_to_time($v[1].' '.$v[2]);
}
// present it
foreach ($arr as $id => $sorted) {
foreach ($sorted as $date => $times) {
// $times = array_map('strtotime', $times); <--commented
$smallest = date('H:i:s', min($times));
$biggest = date('H:i:s', max($times));
echo "ID '{$id}' on '{$date}' SMALLEST TIME is {$smallest}\n";
echo "ID '{$id}' on '{$date}' BIGGEST TIME is {$biggest}\n";
}
}
答案 0 :(得分:1)
您需要先对数组进行分组,然后按ID
分组,然后Date
分组。将它们作为多维数组分配到新容器中,第一级ID
,每个ID
Date
。
点子:
$arr = array();
// group them
foreach ($list_data as $v) {
// first by id, then by date
$arr[$v[0]][$v[1]][] = $v[2];
}
// present it
foreach ($arr as $id => $sorted) {
foreach ($sorted as $date => $times) {
$times = array_map('strtotime', $times);
$smallest = date('H:i:s', min($times));
$biggest = date('H:i:s', max($times));
echo "ID '{$id}' on '{$date}' SMALLEST TIME is {$smallest}<br/>";
echo "ID '{$id}' on '{$date}' BIGGEST TIME is {$biggest}<br/>";
}
}
基本上每次都指定每个日期,只需将它们转换为unix时间戳并作为数组,只需使用min
和max
即可获得最低和最高。