PHP for循环与异常规则

时间:2015-11-11 22:11:01

标签: php loops for-loop

我正在寻找一个应该输出的php for循环 22,23,24,32,33,34等92,93,94和以下所有类似102,103,104,122,123直到902,903,904,992,993,9949,但不是或12,13,14 ...... 112,13,114 ... 912,913,914。 / p>

我的代码:

for ($a = 22; $a <= 1000; $a+=10) {
    $b = $a  + 1;
    $c = $a  + 2;   
    echo "$a <br>";
    echo "$b <br>";
    echo "$c <br>";
}

我需要一个例外,因为112,123和124等等直到912,913,914不应该被回应。 所有结果都应存储在数组中。

2 个答案:

答案 0 :(得分:1)

尝试使用此代码:

$data = array();
for ($a = 2; $a < 10; $a++) {
  for ($b = 2; $b <= 4; $b++) {
    $c = ($a * 10) + $b;
    $data[] = $c;
  }
}

答案 1 :(得分:0)

我发现除了我的例外,所有部分都完成kwestgrounds answer

$delete = array_merge(range (112, 912, 100), range (113, 913, 100),range (114, 914, 100));
$data = array();
for ($a = 2; $a < 100; $a++) {
  for ($b = 2; $b <= 4; $b++) {       
    $c = ($a * 10) + $b;
{
    if (in_array($c, $delete)) continue;
}         
    $data[] = $c;
  }
}