Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中显示一次重复值_Php_If Statement_Foreach - Fatal编程技术网

如何在PHP中显示一次重复值

如何在PHP中显示一次重复值,php,if-statement,foreach,Php,If Statement,Foreach,我有以下代码: foreach($this->authors as $autor){ if($autor->author == $filter_autor){ echo '<option value="'.$autor->author.'" selected="selected">'.$autor- >author.'</option>'; }else{ echo '<option

我有以下代码:

foreach($this->authors as $autor){

    if($autor->author == $filter_autor){
        echo '<option value="'.$autor->author.'" selected="selected">'.$autor-     >author.'</option>';

    }else{
        echo '<option value="'.$autor->author.'">'.$autor->author.'</option>';
      }
};
foreach($this->authors as$autor){
如果($autor->author==$filter\u autor){
回显“.$autor->author.”;
}否则{
回显“.$autor->author.”;
}
};
作者可能多次重复。如何更改此代码,使其只打印重复显示一次的每个代码


提前感谢您的帮助。

未经测试,但请尝试以下操作:

foreach(array_unique($this->authors) as $autor){

    if($autor->author == $filter_autor){
        echo '<option value="'.$autor->author.'" selected="selected">'.$autor-     >author.'</option>';

    }else{
        echo '<option value="'.$autor->author.'">'.$autor->author.'</option>';
      }
};
foreach(数组_unique($this->authors)作为$autor){
如果($autor->author==$filter\u autor){
回显“.$autor->author.”;
}否则{
回显“.$autor->author.”;
}
};

最好的选择是在开始输出之前过滤数组

如果不可能,无论出于何种原因(包括个人偏好),请尝试以下方式:

$used_authors = array();
foreach($this->authors as $autor){
  if(!isset($used_authors[$autor->author]){
    if($autor->author == $filter_autor){
        echo '<option value="'.$autor->author.'" selected="selected">'.$autor-     >author.'</option>';

    }else{
        echo '<option value="'.$autor->author.'">'.$autor->author.'</option>';
    }
    $used_authors[$autor->author] = true;
  }
};
$used_authors=array();
foreach($this->authors as$autor){
如果(!isset($used_authors[$autor->author]){
如果($autor->author==$filter\u autor){
回显“.$autor->author.”;
}否则{
回显“.$autor->author.”;
}
$used_authors[$autor->author]=true;
}
};
$var='';
foreach($this->authors as$autor){
如果($var!=$autor->author)
{
如果($autor->author==$filter\u autor){
回显“.$autor->author.”;
}否则{
回显“.$autor->author.”;
}
}
$var=$autor->author;
};

希望这对您有所帮助

$newarray=array_unique($this->authors);
    foreach($newarray as $autor){

        if($autor->author == $filter_autor){
            echo '<option value="'.$autor->author.'" selected="selected">'.$autor-     >author.'</option>';

        }else{
            echo '<option value="'.$autor->author.'">'.$autor->author.'</option>';
          }
    }
$newarray=array\u unique($this->authors);
foreach($newarray作为$autor){
如果($autor->author==$filter\u autor){
回显“.$autor->author.”;
}否则{
回显“.$autor->author.”;
}
}

了解

希望这对您有所帮助

 $unieq_array=array_unique($this->authors);

        foreach($unieq_array as $autor){

            if($autor->author == $filter_autor){
                echo '<option value="'.$autor->author.'" selected="selected">'.$autor-     >author.'</option>';

            }else{
                echo '<option value="'.$autor->author.'">'.$autor->author.'</option>';
              }
        };
$unieq\u array=array\u unique($this->authors);
foreach($unieq_数组作为$autor){
如果($autor->author==$filter\u autor){
回显“.$autor->author.”;
}否则{
回显“.$autor->author.”;
}
};
 $unieq_array=array_unique($this->authors);

        foreach($unieq_array as $autor){

            if($autor->author == $filter_autor){
                echo '<option value="'.$autor->author.'" selected="selected">'.$autor-     >author.'</option>';

            }else{
                echo '<option value="'.$autor->author.'">'.$autor->author.'</option>';
              }
        };