我有七天的时间。我想打印这个时间,但想要显示相同的开始和结束时间。
例如,如果周一到周五有相同的时间,我应该得到这个结果:
Mon-Fri 9:00 to 17:00
如果没有开始或结束时间,我应该得到这个结果:
Sat-Sun Off Day.
这是我的数据:
Array (
[0] =>
Array (
[customer_id] => 4
[start_time] => 01:00
[end_time] => 16:00
[break_start] => 05:00
[break_end] => 10:00
[off_days] =>
[time_interval] =>
[day] => saturday
[closed] =>
[break] =>
)
[1] =>
Array (
[customer_id] => 4
[start_time] => 04:00
[end_time] => 17:00
[break_start] => 09:00
[break_end] => 10:00
[off_days] =>
[time_interval] =>
[day] => sunday
[closed] =>
[break] =>
)
[2] =>
Array (
[customer_id] => 4
[start_time] => 15:00
[end_time] => 21:00
[break_start] => 17:00
[break_end] => 18:00
[off_days] =>
[time_interval] =>
[day] => monday
[closed] =>
[break] =>
)
[3] =>
Array (
[customer_id] => 4
[start_time] => 00:00
[end_time] =>
[break_start] => 00:00
[break_end] => 00:00
[off_days] =>
[time_interval] =>
[day] => tuesday
[closed] => 1
[break] =>
)
[4] =>
Array (
[customer_id] => 4
[start_time] => 00:00
[end_time] =>
[break_start] => 00:00
[break_end] => 00:00
[off_days] =>
[time_interval] =>
[day] => wednesday
[closed] => 1
[break] =>
)
[5] =>
Array (
[customer_id] => 4
[start_time] => 00:00
[end_time] =>
[break_start] => 00:00
[break_end] => 00:00
[off_days] =>
[time_interval] =>
[day] => thursday
[closed] => 1
[break] =>
)
[6] =>
Array (
[customer_id] => 4
[start_time] => 00:00
[end_time] =>
[break_start] => 00:00
[break_end] => 00:00
[off_days] =>
[time_interval] =>
[day] => friday
[closed] => 1
[break] =>
)
)
答案 0 :(得分:1)
function renderDays($list){
$times=array();
foreach($list as $key=>$item){
@$times[$item["day"]]["start_time"]=$item["start_time"];
@$times[$item["day"]]["end_time"]=$item["end_time"];
}
$time_days=array("mon","tue","wed","thu","fri","sat","sun");
$time_simple=array();
$group_days=array();
$time_index=0;
for ($i=0; $i<6; $i++) {
if ($times[$time_days[$i]]==$times[$time_days[$i+1]]) {
array_push($group_days,$i+1);
if (!in_array($i,$group_days)) {
array_push($group_days,$i);
}
sort($group_days);
} else {
if (count($group_days)>1) {
$group_name=$time_days[$group_days[0]]."-".$time_days[$group_days[count($group_days)-1]];
$time_simple[$group_name]=$times[$time_days[$group_days[0]]];
}
//dont repeat if this week day already is in a group
if (!in_array($times[$time_days[$i]],$time_simple )) {
$time_simple[$time_days[$i]]=$times[$time_days[$i]];
}
$group_days=array();
//reset the group
}
}
//last verification if any group is left
if (count($group_days)>1) {
$group_name=$time_days[$group_days[0]]."-".$time_days[$group_days[count($group_days)-1]];
$time_simple[$group_name]=$times[$time_days[$group_days[0]]];
}
return $time_simple;
}
然后你使用:
$output=renderDays($list);
//render final result as html
foreach($output as $days=>$values){
if (!empty($values["start_time"]) && !empty($values["end_time"])) {
echo $days." ".$values["start_time"]." to ".$values["end_time"];
} else {
echo $days." Off Day";
}
echo "\n";
}
对于$ list,我使用了这个测试数组:
$h=array("day"=>"mon","start_time" => "01:01", "end_time" => "16:00");
$h2=array("day"=>"tue","start_time" => "01:00", "end_time" => "16:00");
$h3=array("day"=>"wed","start_time" => "01:00", "end_time" => "16:00");
$h4=array("day"=>"thu","start_time" => "", "end_time" => "");
$h5=array("day"=>"fri","start_time" => "01:00", "end_time" => "16:00");
$h6=array("day"=>"sat","start_time" => "01:00", "end_time" => "16:00");
$h7=array("day"=>"sun","start_time" => "01:00", "end_time" => "16:00");
$list=array();
array_push($list,$h);
array_push($list,$h2);
array_push($list,$h3);
array_push($list,$h4);
array_push($list,$h5);
array_push($list,$h6);
array_push($list,$h7);
<强>输出强>
星期一01:01至16:00
星期二01:00至16:00
周日休息日fri-sun 01:00至16:00