php php代码,给foreach循环添加条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4517604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:17:12 来源:igfitidea点击:
Php code, add a condition to foreach loop
提问by webmasters
How can i make this code work? TY!
我怎样才能使这段代码工作?泰!
$site = '1'
$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite)
{
echo $mysites; **but not the site with value 1**
}
回答by Jan Han?i?
A simple if
will suffice:
一个简单的if
就足够了:
$site = '1';
$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite)
{
if ( $mysite !== '1' )
{
echo $mysite;
}
}
or if you wan't to check against the $site
variable:
或者如果您不想检查$site
变量:
$site = '1';
$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite)
{
if ( $mysite !== $site )
{
echo $mysite;
}
}
回答by Vladyslav at AssuredLabs
$site = '1'
$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite) {
if ($mysite == $site) { continue; }
// ...your code here...
}
回答by fire
Just use an if
statement:
只需使用一个if
语句:
foreach($mysites as $mysite) {
if ($mysite !== $site) {
echo $mysite;
}
}