0

A类时间:

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

B类时间:

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

如何检查两个班级时间冲突

4

1 回答 1

0

因为重叠时段可能不是简单的==比较,所以类时段必须分成开始和结束块并单独处理。不幸的是,这会产生一个循环繁重的代码块——但这是不可避免的。

array_walk_recursive()用于准备您的输入数据。根据设计,该函数仅迭代的“叶节点” $sched——这意味着它$v永远不会是一个数组,它永远是一个时间跨度的字符串。该函数也是“深度无知”的,因此正确修改原始$sched数据的唯一方法是通过引用使其$v可修改(参见:&$v)。

一旦数据被重组,是时候变得循环了。一系列 3 个连续循环遍历“目标”类和天子数组以进行比较。另外 2 个循环用于访问所有其他类的相同 Day 子数组(句点)。再次实施早期代码构建array_walk_recursive()以减少总循环,但这不是最佳实践,因为使用带有提前退出的 foreach 循环可以break(2)节省迭代并提高效率。

假设某些课程可能没有带句点的一天,我已经进行了一天检查,以确保我的方法不会基于缺少的 Day 子数组导致通知/警告。

代码:(演示

$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);
于 2017-08-02T23:54:02.863 回答