0

I need a way to check if there are events that overlap each other. So I made an array with the start and end hour of every event. It looks like this:

Array
(
    [0] => Array
        (
            [start] => 0930
            [end] => 1200
        )

    [1] => Array
        (
            [start] => 1000
            [end] => 1230
        )

    [2] => Array
        (
            [start] => 1300
            [end] => 1530
        )

)

This is what I've tried to check if there are events that overlap:

if ( $orders->have_posts() ):
        while ($orders->have_posts()) : $orders->the_post(); 
          foreach($order as $o){
            $start = $o['start'];
            $end = $o['end'];
            $attractieID = $o['attractie']->ID;

            foreach($order as $key => $attractieID){
                $slots[] = array('start' => $start, 'end' => $end);
                if($start < $end){
                   //overlap
                }else{
                   //no overlap
                }
            }
          }
        endwhile; 
endif;

But this will always give true since I am checking the start and end date of the same item in my array.

I need to way to compare the start value of the current array item and the end value of the previous array item

Anyone knows if this is even possible?

Many thanks in advance!

5
  • You can't start the body of if with { and end it with endif. Commented Apr 18, 2016 at 15:02
  • @Barmar this is just a typo with copying my code Commented Apr 18, 2016 at 15:04
  • @FrankLucas - Thing about copying code is, it doesn't produce typos. Give actual code. Commented Apr 18, 2016 at 15:05
  • @Pamblam there's a lot of other stuff in my loop that is unrelevant to the question why should I copy it aswel? Commented Apr 18, 2016 at 15:07
  • copy + delete doesn't produce typos either.... copy + re-write will not only produce typs but will make it difficult for people to answer if one of you "typos" obscures the issue. don't re-write your code just for SO :) Commented Apr 18, 2016 at 15:10

3 Answers 3

1

Start looping at index 1, and compare the start time of the current event with the end of the event with index-1.

$count = count($order);
for ($i = 1; $i < $count; $i++) {
    if ($order[$i]['start'] < $order[$i-1]['end']) {
        // overlap
    } else {   
        // no overlap
    }
}

If you want to do this while also copying from $order to slots, you can use a variable to hold the end time from the previous iteration.

  $prevEnd = null;
  foreach($order as $o){
    $start = $o['start'];
    $end = $o['end'];
    $attractieID = $o['attractie']->ID;

    $slots[] = array('start' => $start, 'end' => $end);
    if($prevEnd !== null && $start < $prevEnd){
       //overlap
    }else{
       //no overlap
    }
    $prevEnd = $end;
  }

DEMO

Sign up to request clarification or add additional context in comments.

13 Comments

What does that have to do with checking for overlap?
Because my start and end values are stored in my slots array
Aren't they stored in the $order array?
Yes but I need to do a check based on the attractie id see the $attractieID = $o['attractie']->ID; in OP
In an order you can have multimple events(attracties) every attractie has a start and end value. And I need to check for every event(attractie) if there are overlapping events(attracties)
|
0

Try this code, with a for loop instead of foreach:

for($i=0;$i<count($order);i++){
  $slots[] = array('start' => $start, 'end' => $end);
   if($order[$i+1]){
     if($order[$i]['start'] < $order[$i+1]['end']{
       //overklap
     }else{
      //no overlap
     }
   }

Comments

0

Just use a normal for loop. And, to be sure everything is right, sort the array before checking it (if you are sure it is gona be sorted, you can skip that part)

$sortedOrders = usort($order, function($a, $b)
{
    $aStart = $a['start'];
    $bStart = $b['start'];

    return ($a < $b) ? -1 : 1;  
});

$count = count($sortedOrders);
for($i = 1; $i < $count; $i++)
{
    if($sortedOrders[$i - 1]['end'] > $sortedOrders[$i]['start'])
    {
        // Handle overlap
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.