1

I have an array where I store an ID and for every ID there's an array that holds a start and end value. My goal is to check if the start value is smaller than the end value of the item that comes before it in the array.

This works but my loop doesn't care about the ID's, I need a way to do this per ID

Here's what my array looks like:

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

                )

        )

    [897] => Array
        (
            [41] => Array
                (
                    [0] => Array
                        (
                            [start] => 1000
                            [end] => 1230
                        )

                )

        )

    [898] => Array
        (
            [41] => Array
                (
                    [0] => Array
                        (
                            [start] => 1100
                            [end] => 1300
                        )

                )

        )

    [901] => Array
        (
            [52] => Array
                (
                    [0] => Array
                        (
                            [start] => 0930
                            [end] => 1030
                        )

                )

        )

    [903] => Array
        (
            [52] => Array
                (
                    [0] => Array
                        (
                            [start] => 0930
                            [end] => 1030
                        )

                )

        )

    [904] => Array
        (
            [41] => Array
                (
                    [0] => Array
                        (
                            [start] => 1000
                            [end] => 1230
                        )

                )

        )

)

And this is what my loop looks like:

$foundOverlap = false;
$prevEnd = null;
foreach($slots as $s){

     $startHour = $s[0]['start'];
     $endHour = $s[0]['end'];
     echo $startHour . '<br>';
     echo $endHour . '<br>';

     if($prevEnd !== null && $startHour < $prevEnd){
         $foundOverlap = true;
         $counter++;
     } 

     if($foundOverlap){
        echo 'overlap <br>';
     }else{
        echo 'no overlap <br>';
     }

     $prevEnd = $endHour;

}

As a result I get No overlap(good), overlap(good), overlap(good), overlap(BAD!) this should be no overlap. But it says overlap because It checks with the previous array item doesn't care what the attractieID is...

Anyone please knows how I can fix this?

I would be soooo thankful!!

Thanks in advance!

Frank Lucas
  • 582
  • 2
  • 12
  • 28
  • You have already asked the same question today. Here is the link (http://stackoverflow.com/questions/36714467/php-check-values-of-array-per-id) – Nehal Apr 19 '16 at 12:14
  • @Ms.Nehal I deleted that question since there were some errors with my code – Frank Lucas Apr 19 '16 at 12:15
  • 1
    save `attractieID` in a variable e.g. `$preID` and update it like `$prevEnd` Then in `if($prevEnd !== null && $startHour < $prevEnd && $currentID == $preID)` – Abbasi Apr 19 '16 at 12:21
  • @MuhammadKashifAbbasi What should the `$currentID` be? – Frank Lucas Apr 19 '16 at 12:23
  • atr to reset `$foundOverlap = true;` every time. use `$foundOverlap = false;` to your foreach loop everytime. – Murad Hasan Apr 19 '16 at 12:29
  • @FrayneKonok I am sorry but I do not understand what you're trying to say. – Frank Lucas Apr 19 '16 at 12:30
  • check out [my answer](http://stackoverflow.com/questions/36717999/php-check-if-value-in-array-is-smaller-than-previous-value-in-array-per-id/36718521#36718521) to see what i am saying?? – Murad Hasan Apr 19 '16 at 12:33

2 Answers2

0

From Muhammad Kashif Abbasi's comment I would like to give you a solution

Try this code

$foundOverlap = false;
$prevEnd = null;
$preID = null;
foreach($slots as $s){

     $startHour = $s[0]['start'];
     $endHour = $s[0]['end'];
     $currentID = $s['attractieID']; //got the current attractieID
     echo $startHour . '<br>';
     echo $endHour . '<br>';

     if($prevEnd !== null && $startHour < $prevEnd && $currentID == $preID){ // checked current id and previous id
         $foundOverlap = true;
         $counter++;
     } 

     if($foundOverlap){
        echo 'overlap <br>';
     }else{
        echo 'no overlap <br>';
     }
     $foundOverlap = false; //reseted $foundOverlap
     $prevEnd = $endHour;
     $preID = $currentID; //set the current id as previous id

}
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
0

check this out

$result = [];
foreach($array as $element) {
    if(!isset($result[$element['attractiveID']])) {
        $result[$element['attractiveID']]['overlap'] = false;
        $result[$element['attractiveID']]['prevHour'] = $element[0]['end'];
        continue;
    }

    if($element[0]['start'] < $result[$element['attractiveID']]['prevHour']) {
        $result[$element['attractiveID']]['overlap'] = true;
    }

    $result[$element['attractiveID']]['prevHour'] = $element[0]['end'];
}

and array passed:

$array = [
    [
        'attractiveID' => 41,
        [
            'start' => 930,
            'end' => 1200
        ]
    ],
    [
        'attractiveID' => 41,
        [
            'start' => 1000,
            'end' => 1230
        ]
    ],
    [
        'attractiveID' => 41,
        [
            'start' => 1100,
            'end' => 1300
        ]
    ],
    [
        'attractiveID' => 52,
        [
            'start' => 1000,
            'end' => 1230
        ]
    ],
];
Lanah
  • 155
  • 6