Hello! Tell me the algorithm for adding time intervals. That is, we assume an array of intervals:
09:00 - 12:00
13:15 - 14:00
12:00 - 13:00
It is necessary to get the output array like this:
09:00 - 13:00
13:15 - 14:00
That is, if the intervals lie nearby, then they merge into one (in the example, they merge and turn out to be 09:00 - 13:00).
I only got this code:
$array[] = array('start'=>'12:00', 'end'=>'13:00'); $array[] = array('start'=>'09:00', 'end'=>'12:00'); $array[] = array('start'=>'13:15', 'end'=>'14:00'); $newints = array(); foreach($array as $arr) { foreach($newints as $key=>$nint) { if($arr['start']==$nint['end']) { $newints[$key]['end'] = $arr['end']; echo 'test'; continue 2; } if($arr['end']==$nint['start']) { $newints[$key]['start'] = $arr['start']; continue 2; } } $newint['start'] = $arr['start']; $newint['end'] = $arr['end']; $newints[] = $newint; } var_dump($newints);
It seems to work correctly, but in my opinion, the code could be more elegant, and therefore some nuances are probably concealed due to which there may be bugs. Please, help)