如何跳过PHP中的循环迭代? [英] How to skip iterations in a for loop in PHP?
本文介绍了如何跳过PHP中的循环迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选项列表(预订座位),我想从中排除某些值(例如3,4,8和19)。我用来构建列表的代码是:
$ p $ <?php
for($ i = 1; $ i <= 27; $ i ++)
{
echo< option value = $ i> $ i< / option>;
}
?>
如何从列表中排除3,4,8和19? $ b
解决方案
您可以使用 continue
跳过循环的当前迭代。 b
$ b
$ exclude = array(3,4,8,19); ($ i = 1; $ i <= 27; $ i ++)
if(in_array($ i,$ exclude))continue;
echo< option value = $ i> $ i< / option>;
I have a list of options (booked seats) from which I want to exclude certain values (e.g., 3, 4, 8 and 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?
解决方案
You can use continue
to skip the current iteration of a loop.
$exclude = array(3, 4, 8, 19);
for ($i=1; $i<=27; $i++)
{
if (in_array($i, $exclude)) continue;
echo "<option value=$i>$i</option>";
}
这篇关于如何跳过PHP中的循环迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文