简体   繁体   English

如何在PHP的for循环中跳过迭代?

[英]How to skip iterations in a for loop in PHP?

I have a list of options (booked seats) from which I want to exclude certain values (eg, 3, 4, 8 and 19). 我有一个选项列表(预定座位),我想从中排除某些值(例如3、4、8和19)。 The code I have for constructing the list is: 我用于构造列表的代码是:

<?php
for ($i=1; $i<=27; $i++)
  {
    echo "<option value=$i>$i</option>";
  }
?>

How do I exclude 3, 4, 8 and 19 from the list? 如何从列表中排除3、4、8和19?

You can use continue to skip the current iteration of a loop. 您可以使用continue跳过循环的当前迭代。

$exclude = array(3, 4, 8, 19);

for ($i=1; $i<=27; $i++)
{
    if (in_array($i, $exclude)) continue;
    echo "<option value=$i>$i</option>";
}

Documentation . 文件资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM