I have an array $dice (4,7,3,6,7)
I need a way of checking if each of the values in that array are consecutive numbers.
Is there an easy method of doing this?
try this:
$dice = array(4,5,2,6,7);
function checkConsec($d) {
for($i=0;$i<count($d);$i++) {
if(isset($d[$i+1]) && $d[$i]+1 != $d[$i+1]) {
return false;
}
}
return true;
}
var_dump(checkConsec($dice)); //returns false
var_dump(checkConsec(array(4,5,6,7))); //returns true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With