Php代码,为foreach循环添加一个条件

Php代码,为foreach循环添加一个条件,php,foreach,Php,Foreach,我怎样才能使这个代码工作?泰 $site = '1' $mysites = array('1', '2', '3', '4', '5', '6'); foreach($mysites as $mysite) { echo $mysites; **but not the site with value 1** } 只需使用if语句: foreach($mysites as $mysite

我怎样才能使这个代码工作?泰

$site = '1'

    $mysites = array('1', '2', '3', '4', '5', '6');
            foreach($mysites as $mysite) 
            {
            echo $mysites;  **but not the site with value 1**
            }

只需使用
if
语句:

foreach($mysites as $mysite) {
    if ($mysite !== $site) {
        echo $mysite;
    }
}

一个简单的
if
就足够了:

$site = '1';

$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite) 
{
    if ( $mysite !== '1' )
    {
        echo $mysite;
    }
}
或者如果您不想检查
$site
变量:

$site = '1';

$mysites = array('1', '2', '3', '4', '5', '6');
foreach($mysites as $mysite) 
{
    if ( $mysite !== $site )
    {
        echo $mysite;
    }
}
您可以这样做:

$mysites = array('1', '2', '3', '4', '5', '6');
        foreach($mysites as $mysite) 
        {
if($mysite != 1){
        echo $mysite;
}
        }

无论如何,
$site='1'
无效,
$site='1'是。另外,
echo$mysites$mysites
是一个
数组,因此code>不会显示任何内容。请不要为了更快地回答问题而复制/粘贴OP问题中的错误代码。@ClemDesm我已经修复了这些错误。我不打算尽快回答,我只是完全错过了那些错误。谢谢你指出它们!这是一段非常小而简单的代码,它直接回答了问题,这样一段简单的代码几乎是不言自明的。。。如果不是的话,你能给我一个建议吗?对于这个问题,什么样的解释才是合适的呢。。你是对的。我道歉,是的这就是问题所在,现在已经解决了。结果不一样,你可以观看这个链接
$mysites = array('1', '2', '3', '4', '5', '6');
        foreach($mysites as $mysite) 
        {
if($mysite != 1){
        echo $mysite;
}
        }