如果PHP校验序列值是连续 [英] php check if array values are consecutive
本文介绍了如果PHP校验序列值是连续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组 $骰子(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?
推荐答案
试试这个:
$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
这篇关于如果PHP校验序列值是连续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文