如何检查每天的时间跨度冲突?
How can I check for time span conflicts on each day?
Class 一次:
Day 1 => 08:00-09:00 | 11:00-12:00 | 13:00-15:00 | 20:00-21:00
Day 2 => 12:00-13:00 | 14:00-15:00
Day 3 => 08:00-09:00 | 16:00-17:00 | 18:00-19:00
Day 4 => 10:00-11:00 | 12:00-14:00 | 14:00-16:00
Day 5 => 08:00-10:00 | 13:00-15:00
Day 6 => 80:00-09:00 | 10:00-11:00
Class乙次:
Day 1 => 10:00-11:00 | 17:00-19:00
Day 2 => 08:00-09:00 | 11:00-12:00
Day 3 => 08:00-09:00 | 17:00-18:00 | 20:00-21:00
Day 4 => 08:00-09:00 | 14:00-16:00 | 17:00-18:00
Day 5 => 08:00-10:00 | 14:00-15:00
Day 6 => 10:00-11:00
How to check the two class time conflict
因为重叠周期可能不是简单的==
比较,class周期必须分成开始和结束块并单独处理。不幸的是,这会产生大量循环的代码块——但这是不可避免的。
array_walk_recursive()
用于准备您的输入数据。根据设计,该函数仅迭代 $sched
的 "leaf-nodes" —— 这意味着 $v
永远不会是数组,它始终是时间跨度字符串。该函数也是 "depth-ignorant",因此正确修改原始 $sched
数据的唯一方法是使 $v
可通过引用修改(参见:&$v
)。
一旦数据被重组,就该变得疯狂了。一系列 3 个连续循环遍历 "targeted" Class 和 Day 子数组以进行比较。另外 2 个循环用于访问所有其他 Classes 的相同 Day 子数组(周期)。早期的代码构建再次实现 array_walk_recursive()
以减少总循环,但这不是最佳实践,因为使用带有 break(2)
提前退出的 foreach 循环可以节省迭代次数并提高效率。
假设某些 classes 可能 没有一天有月经,我已经进行了 Day 检查以确保我的方法不会导致 Notices/Warnings 基于缺失的 Day 子数组。
代码:(Demo)
$sched=[
'Class A'=>[
'Day 1'=>['10:00-11:00','11:00-12:00','13:00-15:00','20:00-21:00'],
'Day 2'=>['12:00-13:00','14:00-15:00'],
'Day 3'=>['08:00-09:00','16:00-17:00','18:00-19:00'],
'Day 4'=>['10:00-11:00','12:00-14:00','14:00-16:00'],
'Day 5'=>['08:00-10:00','13:00-15:00'],
'Day 6'=>['08:00-09:00','10:00-11:00']
],
'Class B'=>[
'Day 1'=>['09:30-10:30','17:00-19:00'],
'Day 2'=>['08:00-09:00','11:00-12:00'],
'Day 3'=>['08:00-09:00','17:00-18:00','20:00-21:00'],
'Day 4'=>['08:00-09:00','14:00-16:00','17:00-18:00'],
'Day 5'=>['08:00-10:00','14:00-15:00'],
'Day 6'=>['10:00-11:00']
],
'Class C'=>[
'Day 1'=>['12:00-14:00']
]
];
// Preparation: Split the dashed time spans into chunked subarrays containing start and end times
// e.g. $sched['Class A']['Day 1'][['10:00','11:00'],['17:00','19:00']]
array_walk_recursive($sched,function(&$v){$v=explode('-',$v);});
// Comparisons:
foreach($sched as $class=>$days){
$other_classes=array_diff_key($sched,[$class=>'']); // same array without target class
foreach($days as $day=>$spans){
//echo "\n\n$class, $day";
foreach($spans as $period=>$span){
//echo "\nchecking {$span[0]} & {$span[1]}";
foreach($other_classes as $other_class=>$other_days){
if(isset($other_days[$day])){ // this avoids Notices/Warning about non-existent day in class
foreach($other_days[$day] as $other_period=>$other_span){ // compare other classes on same day
//echo "\nversus {$other_span[0]} & {$other_span[1]}";
if(($span[0]>=$other_span[0] && $span[0]<$other_span[1]) || ($span[1]<=$other_span[1] && $span[1]>$other_span[0])){
$result[]="Clash: $class,$day#".++$period.": {$span[0]}-{$span[1]} vs $other_class,$day#".++$other_period.": {$other_span[0]}-{$other_span[1]}";
break(2); // no need to further check this $span for clashes
}
}
}
}
}
}
}
echo implode("\n",$result);
Class 一次:
Day 1 => 08:00-09:00 | 11:00-12:00 | 13:00-15:00 | 20:00-21:00
Day 2 => 12:00-13:00 | 14:00-15:00
Day 3 => 08:00-09:00 | 16:00-17:00 | 18:00-19:00
Day 4 => 10:00-11:00 | 12:00-14:00 | 14:00-16:00
Day 5 => 08:00-10:00 | 13:00-15:00
Day 6 => 80:00-09:00 | 10:00-11:00
Class乙次:
Day 1 => 10:00-11:00 | 17:00-19:00
Day 2 => 08:00-09:00 | 11:00-12:00
Day 3 => 08:00-09:00 | 17:00-18:00 | 20:00-21:00
Day 4 => 08:00-09:00 | 14:00-16:00 | 17:00-18:00
Day 5 => 08:00-10:00 | 14:00-15:00
Day 6 => 10:00-11:00
How to check the two class time conflict
因为重叠周期可能不是简单的==
比较,class周期必须分成开始和结束块并单独处理。不幸的是,这会产生大量循环的代码块——但这是不可避免的。
array_walk_recursive()
用于准备您的输入数据。根据设计,该函数仅迭代 $sched
的 "leaf-nodes" —— 这意味着 $v
永远不会是数组,它始终是时间跨度字符串。该函数也是 "depth-ignorant",因此正确修改原始 $sched
数据的唯一方法是使 $v
可通过引用修改(参见:&$v
)。
一旦数据被重组,就该变得疯狂了。一系列 3 个连续循环遍历 "targeted" Class 和 Day 子数组以进行比较。另外 2 个循环用于访问所有其他 Classes 的相同 Day 子数组(周期)。早期的代码构建再次实现 array_walk_recursive()
以减少总循环,但这不是最佳实践,因为使用带有 break(2)
提前退出的 foreach 循环可以节省迭代次数并提高效率。
假设某些 classes 可能 没有一天有月经,我已经进行了 Day 检查以确保我的方法不会导致 Notices/Warnings 基于缺失的 Day 子数组。
代码:(Demo)
$sched=[
'Class A'=>[
'Day 1'=>['10:00-11:00','11:00-12:00','13:00-15:00','20:00-21:00'],
'Day 2'=>['12:00-13:00','14:00-15:00'],
'Day 3'=>['08:00-09:00','16:00-17:00','18:00-19:00'],
'Day 4'=>['10:00-11:00','12:00-14:00','14:00-16:00'],
'Day 5'=>['08:00-10:00','13:00-15:00'],
'Day 6'=>['08:00-09:00','10:00-11:00']
],
'Class B'=>[
'Day 1'=>['09:30-10:30','17:00-19:00'],
'Day 2'=>['08:00-09:00','11:00-12:00'],
'Day 3'=>['08:00-09:00','17:00-18:00','20:00-21:00'],
'Day 4'=>['08:00-09:00','14:00-16:00','17:00-18:00'],
'Day 5'=>['08:00-10:00','14:00-15:00'],
'Day 6'=>['10:00-11:00']
],
'Class C'=>[
'Day 1'=>['12:00-14:00']
]
];
// Preparation: Split the dashed time spans into chunked subarrays containing start and end times
// e.g. $sched['Class A']['Day 1'][['10:00','11:00'],['17:00','19:00']]
array_walk_recursive($sched,function(&$v){$v=explode('-',$v);});
// Comparisons:
foreach($sched as $class=>$days){
$other_classes=array_diff_key($sched,[$class=>'']); // same array without target class
foreach($days as $day=>$spans){
//echo "\n\n$class, $day";
foreach($spans as $period=>$span){
//echo "\nchecking {$span[0]} & {$span[1]}";
foreach($other_classes as $other_class=>$other_days){
if(isset($other_days[$day])){ // this avoids Notices/Warning about non-existent day in class
foreach($other_days[$day] as $other_period=>$other_span){ // compare other classes on same day
//echo "\nversus {$other_span[0]} & {$other_span[1]}";
if(($span[0]>=$other_span[0] && $span[0]<$other_span[1]) || ($span[1]<=$other_span[1] && $span[1]>$other_span[0])){
$result[]="Clash: $class,$day#".++$period.": {$span[0]}-{$span[1]} vs $other_class,$day#".++$other_period.": {$other_span[0]}-{$other_span[1]}";
break(2); // no need to further check this $span for clashes
}
}
}
}
}
}
}
echo implode("\n",$result);