SPQRInc
SPQRInc

Reputation: 198

Check, if results are continuous

I got a little thing to solve with MySQL and PHP.

Let's assume I got a table with this rows:

Now I would like to check, if there are 5 or more entries that have continuously the value false.

Example:

id | value | date
1 - true - 01.01.1970
2 - false - 02.01.1970
3 - false - 03.01.1970
4 - false - 04.01.1970
5 - false - 05.01.1970
6 - false - 06.01.1970

Here we have 5 entries that are continuously false. So my script should return true (yes, there are 5 or more entries, that are continuously false).

This example should return false:

id | value | date
1 - false - 01.01.1970
2 - false - 02.01.1970
3 - true - 03.01.1970
4 - false - 04.01.1970
5 - true - 05.01.1970
6 - false - 06.01.1970

This should return false (no, there are not 5 or more entries continuously false).

Any idea how to solve this?

Upvotes: 1

Views: 221

Answers (1)

Touqeer Shafi
Touqeer Shafi

Reputation: 5284

Yes it's possible using loop eg:

$array = [[1, true], [1, false], [1, false],[1, false],[1, false],[1, false]]; // Your array

$counter = 0;
$hasFoundFiveContinuesFalse = false;
foreach($array as $item) {
    if($item[1] == false) {
        $counter++;
    } else {
        $counter = 0;
    }

    if($counter >= 5 ) {
        $hasFoundFiveContinuesFalse = true; // if we have found 5 times false then break the loop and set hasFoundFiveContinuesFalse to true 
        break;
    }
}

var_dump($hasFoundFiveContinuesFalse);

This might not be a perfect way but it should work.

Upvotes: 1

Related Questions