PHP检查值是否是具有共同差异的数字序列的一部分

I'm writing a script that needs to identify every 7th number where the first number of the sequence is 0 (so the sequence is 6, 13, 20, 27, 34, 41, 48, etc.)

Here's what I've got so far (which works in a limited way), but I'd prefer to write a loop so that the values in the sequence don't have to be hardcoded:

<?php

$x = 7;

if ($x === 6 || $x === 13 || $x === 20 || $x === 27 || $x === 34 || $x === 41 || $x === 48) {
    $result = 1;
  } else {
    $result = 0;
}

echo $result;

?>

Thanks in advance for any advice!


Update: Here's my updated script using @robbieaverill 's solution and checking the values against an array. There are many great solutions here, thanks everyone!

<?php

$divisor = 7;
$limit = 500; 

$sequence = array();

for ($i = 0; $i <= $limit; $i++) {
  if (is_int(($i + 1) / $divisor)) {
    $sequence[] = $i;
  }
}

$x = 41;

if (in_array($x, $sequence, true)) {
  $result = 1;
} else {
  $result = 0;
}

echo $result;
?>

The output here is 1, indicating that if the value of $x is 41 it is found within the $sequence array.

There's probably a bunch of different ways you could do this. If you need to identify numbers that are evenly divisible by 7 (plus one, because you're zero based), you could do something like this:

$divisor = 7;
$limit = 100; // when do you want to stop searching?

for ($i = 0; $i <= $limit; $i++) {
    if (is_int(($i + 1) / $divisor)) {
        var_dump($i);
    }
}

Note that is_int() is checking whether the divided result is a whole number, it'd return false for a decimal/floating point number.

A solution like this would work also.

$index = 0;
for($i = 0; $i <= 700; $i++){
    if($index == 6){
        echo $i . "<br/>";
        $index = 0;
    } else {
       $index++; 
    }
}

will output 6 13 20 27 34 41 48 55

And here's another way of doing that using the modulus operator:

<?php
$start = 0;
$sequence = 7;
$limit = 100;

for ($i=$start; $i<$limit; $i++) {
    // if the divition of the current number / the sequence is a full number (there is no remainder) and we're not at 0
    if(($i+1) % $sequence == 0 && $i!=0) {
       echo $i . "<br>";
    }
}

// output:
6
13
20
27
34
41
48
55
62
69
76
83
90
97

Since you said "that needs to identify every 7th number" you maybe only wanna check if a given number is one of a 7th, meaning you want a function to test. Then this could help:

function isMultipleOf($input, $sequence) {
    if(($input+1) % $sequence == 0 && $input!=0) {
        return true;
    } else {
        return false;
    }

}


$check = 601;
echo isMultipleOf($check, $sequence); // true/1;
$check = 2;
echo isMultipleOf($check, $sequence); // false;

In General we could write an arithmetic sequence like this:

{a, a+d, a+2d, a+3d, ... }

Where a = 6 (the first term) d = 7 (the "common difference" between terms)

so we can have {a, a+d, a+2d, a+3d, ... }

{6, 6+7, 6+2×7, 6+3×7, ... }

{6, 13, 20, 27, ... } so we can have a rule

x[n] = a + d(n−1)

which can be written in php as below

<?php
$a=6;
$n=10; //number of terms
$d=7;
for($i=1;$i<=10;$i++){
    echo $term=$a+($i-1)*$d;
    echo "<br>";
}

?>

Add 1 to sequential number $i and use modulus n and return $i % 7 == 0.

E.g.

function isNth(int $i, int $nth, int $offset = -1): bool
{
    return !(($i - $offset) % $nth);
}

Usage example:

foreach ([0,1,2,3,4,5,6,7,8,9,10,11,12,13] as $number)
{
    if (isNth($number, 7)) print "$number
";
}

// will print 6
13


foreach ([1,2,3,4,5,6,7,8,9,10,11,12,13,14] as $number)
{
    if (isNth($number, 7, 0)) print "$number
";
}

// will print 7
14

So if you have a list of number best thing would be

<?php
   $list = array(0,1,8,89,785,0,789785,56,8,9,1,2,3,2);
   for($i = 0; $i < count($list);$i++){
      if($list[$i] === 0){
          echo "Found and occurence. Value is ".($list[$i+7])."<br>";
       }
    }
?>

Will output Found and occurence. Value is 56 Found and occurence. Value is 3

Consider using range to generate the series:

$begin = 6;
$end = 48;
$step = 7;
$seq = range($begin, $end, $step);
print_r($seq);

Then you can check if a number is in that series using in_array:

if (in_array(41, $seq)) // ...

See it live on 3v4l.org.

This is in no way efficient, but it's an obvious implementation that works very well for small series. By small I mean thousands of entries or fewer.