简体   繁体   中英

PHP check if value in array is smaller than previous value in array per ID

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!

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

}

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
        ]
    ],
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM