[英]Exclude Items in a foreach
I have an array, which contents are shown by a foreach. 我有一个数组,其中的内容由foreach显示。 This is my Code:
这是我的代码:
foreach($ga->getResults() as $result)
{
if ($result->getdayOfWeek() != 5 || $result->getdayOfWeek() != 6):
echo $result->getdayOfWeek().'<br>';
endif;
}
I want to exclude Itmes where $result->getdayOfWeek()
is 5 oder 6. The way I have it does not work. 我想排除
$result->getdayOfWeek()
为5或6的$result->getdayOfWeek()
。我使用它的方式不起作用。
You need an and (&&) instead of an or (||). 您需要使用and(&&)而不是or(||)。
foreach($ga->getResults() as $result)
{
if ($result->getdayOfWeek() != 5 && $result->getdayOfWeek() != 6):
echo $result->getdayOfWeek().'<br>';
endif;
}
You could also do it the following way: 您也可以通过以下方式进行操作:
foreach($ga->getResults() as $result)
{
if ($result->getdayOfWeek() == 5 || $result->getdayOfWeek() == 6):
continue;
endif;
echo $result->getdayOfWeek().'<br>';
}
Also, as nikic mentions, the convention is using braces for your if statements: 另外,正如nikic所提到的,约定是在if语句中使用花括号:
if ($result->getdayOfWeek() == 5 || $result->getdayOfWeek() == 6)
{
continue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.