Home > Backend Development > PHP Tutorial > foreach循环样式求帮

foreach循环样式求帮

WBOY
Release: 2016-06-20 12:33:13
Original
1015 people have browsed it

查询sql获得5条数据,我想把获得的这5条数据以以下方式表现,该什么弄呢?
想要达到的效果如下:
arr={你好,我好,大家好,很好,太好了};
foreach arr 后怎么达到下面的输出效果?

你好



  • 我好

  • 大家好



  • 很好

  • 太好了



  • 回复讨论(解决方案)

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

    噢,少了

    $arr = array('你好', '我好', '大家好', '很好', '太好了');foreach($arr as $k=>$v) {  if($k == 0) echo "<dl>$v</dl>\n<div>";  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><li>你好</li><ul id=1><li>我好</li><li>大家好</li></ul><ul id=2><li>很好</li><li>太好了</li></ul><div>
    Copy after login

    if($k == 0)
    后重复的第一条怎么去掉

    Related labels:
    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