Home > Backend Development > PHP Tutorial > xuzuning板主,刚刚foreach的问题还有个问题

xuzuning板主,刚刚foreach的问题还有个问题

WBOY
Release: 2016-06-20 12:33:15
Original
923 people have browsed it

$arr = array('你好', '我好', '大家好', '很好', '太好了');
 
foreach($arr as $k=>$v) {
if($k == 0) echo "

$v
";
echo "
  • $v
  • \n";这个第一条是重复的,怎么去掉?
    }
    输出为:
    你好

  • 你好
  • 这里是重复的,怎么去掉这个呢?
  • 我好

  • 大家好

  • 很好

  • 太好了


  • 回复讨论(解决方案)

    $arr = array('你好', '我好', '大家好', '很好', '太好了'); foreach($arr as $k=>$v) {  if($k == 0) {    echo "<dl>$v</dl>\n<div>";    continue;  }  if($k % 2 == 1) {    if(ceil($k / 2) > 1) echo "</ul>\n";    echo "<ul id=" . ceil($k / 2) . ">\n";  }  echo "<li>$v</li>\n";}echo "</ul>\n<div>";
    Copy after login
    <dl>你好</dl><div><ul id=1><li>我好</li><li>大家好</li></ul><ul id=2><li>很好</li><li>太好了</li></ul><div>
    Copy after login

    最基本的表达式:if...else...

    if (0 == $k) {  echo "<dl>{$v}</dl>";} else {  echo "<li>{$v}</li>";}
    Copy after login

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template