3

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?

3
  • 2
    What do you mean by "consecutive"? Commented Jan 1, 2012 at 21:22
  • if you mean in order, just duplicate the array, sort the duplicate and compare with == Commented Jan 1, 2012 at 22:42
  • @fge, Consecutive definition Commented Jul 20, 2015 at 0:40

6 Answers 6

3

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
Sign up to request clarification or add additional context in comments.

Comments

1
function HasConsec($array)
{
    $res = false;
    $cb = false;
    foreach ($array as $im)
    {
        if ($cb !== false && $cb == $im)
            $res = true;
        $cb = $im + 1;
    }

    return $res;
}

2 Comments

how would I use this? if (HasConsec($dice) =='1') ?
if (HasConsec($dice))... the function returns a boolean
1

I think this that this is what you are looking for

for ($c = 0; $c < count($dice); $c++){
    $next_arr_pos = $c+1;

    if ($c == (count($dice) -1)){
        $cons_check = $dice[$c];
    }
    else
    {
        $cons_check = $dice[$next_arr_pos];
        $gap_calc = $cons_check-$dice[$c];
    }

    if ($dice[$c] < $cons_check && $gap_calc == 1){

        echo 'arr_pos = '.$dice[$c].' Is consecutive with '.$cons_check.' <br />';
    }
}

Comments

1

I came up with another approach:

$nums = [-1,0,1];
$nums = array_unique($nums); # optional, comment on the bottom
$range = range(min($nums), max($nums));
$diff = array_diff($range, $nums);
$isConsecutive = empty($diff) && count($nums) === count($range);

$nums = array_unique($nums) - add this if you consider arrays with duplicated numbers valid too e.g. [-1,0,1,1]

Comments

0

However, I'm not a PHP developer and I know that the question looks very old but there is an easy algorithm which I like to share with you that can tell you if your array is consecutive or not.

Here are the steps:

  • find the maximum number and the minimum number of the input array.

  • apply the equation: (max - min + 1) == array.length.

  • if true, then it is consecutive other wise false.

8 Comments

I just wanted to avoid looping through the input array
array = [1,2,4,3,5,6]; (6 - 1 + 1 == array.length) == true
Which is true as 6 -1 + 1 = 6 which equals the length of the array
But those values are not consecutive was my point. [1,2,3,4,5,6] is an array of consecutive numbers.
but it's consecutive when sorted, that's why you are getting a true I found the algorithms here too: geeksforgeeks.org/check-if-array-elements-are-consecutive
|
0

1// care your RAM!

2// only valid for increments of one by one (3.02, 3.03, 3.04, ... are consecutives!)

$x = ARRAY(9, 999999999);       // be careful!, RAM!
$x = ARRAY(99999997, 99999998, 99999999);

function concq_N($A){
    $tmp = $A[0];
    for($i = 1; $i < count($A); ++ $i){
        ++ $tmp;
        if($tmp != $A[$i])return false;
        }
    return true;
    }

echo(concq_N($x));

maybe a function including $scale_of_increment and $direction will be intersting:

$x = ARRAY(-99999997, -99999998, -99999999);

is consecutive!, but my function return false xD

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.