方式一:
//受檢區間
$check = array(51, 58);
//產生區間陣列
$Tmp = array();
$Tmp[] = range(10, 20);
$Tmp[] = range(30, 50);
$Tmp[] = range(60, 80);
$check_status = false;
foreach ($Tmp as &$arr){
//若陣列產生交集,代表有重覆,直接跳出不檢查
if (array_intersect($arr, $check)){
$check_status = true;
break;
}
}
if ($check_status){
echo "區間有重覆";
}else{
echo "區間不重覆";
}
方式二:
//受檢區間
$check = array(21, 22);
//產生區間陣列(將區間排序後,將起始結束串成偶數陣列)
$Tmp = array(10, 20, 30, 50, 60, 80);
$start = 0;
foreach ($Tmp as $key => &$arr){
//檢查開始值
if ($arr > $check[0]){
$start = $key;
break;
}
}
$end = 0;
foreach ($Tmp as $key => &$arr){
//檢查結束值
if ($arr > $check[1]){
$end = $key;
break;
}
}
//開始及結束落在同區間,且區間為偶數,代表落在2組區間中間,因此為不重覆
if ($start==$end && $start%2==0){
echo "區間不重覆";
}else{
echo "區間有重覆";
}