// 你们也是这样做的吗?$col = 3;$i=0;
foreach($arr as $a)
{
 if ($i%3==0) echo "<tr>";
 echo "<td>" . $a "</td>";
 if ($i%3==0) echo "</tr>";
 $i++;
}

解决方案 »

  1.   

    你的逻辑不对吧,应该是:
    $col = 3; 
    $i=0; 
    $isEnd=0;
    foreach($arr as $a) 

    if ($i%$col==0) {
    echo " <tr>"; 
    $isEnd = 1;
    }
    echo " <td>" . $a " </td>"; 

    if ($i%$col==0 && $isEnd == 1){
    $isEnd = 0;
    echo " </tr>"; 
    }
    $i++; 
    }
      

  2.   

    晕倒,自己脑袋不清楚,也写错了,修改一下:
    $col = 3; 
    $i=0; 
    foreach($arr as $a) 

    if ($i%$col==0)  echo " <tr>"; 
    echo " <td>" . $a " </td>"; 
    if ($i%$col==$col - 1) echo " </tr>"; 
    $i++; 
    }
      

  3.   

    用array_slice比较简单一些,而且楼主的条件判断好像有问题$arr = array(2,3,5,6,5,5,5,6,6,55,6,6,5,5,55,32025);echo "<table border=1>";
    for ($i=0; $i<ceil(count($arr)/3); $i++){
    $result = array_slice($arr, $i*3, 3);
    echo "<tr>";
    foreach($result as $r){
    echo "<td>$r</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
      

  4.   

    CSS来,这种情况用table就是累赘
      

  5.   


    <style>
    div{
    margin:0;
    padding:0;
    }
    .box{
    width:300px;
    }
    .box div{
    width:100px;
    float:left;
    }
    </style>
    <?php
    $arr = array(2,3,5,6,5,5,5,6,6,55,6,6,5,5,55,32025);
    echo '<div class="box"><div>'.implode('</div><div>',$arr).'</div></div>';
      

  6.   

    echo str_repeat("<td>&nbsp;</td>",3-$i%3);
      

  7.   

    $arr = array(1,2,3,4,5,6,7,8,9,10);
    $col = 3; 
    $i=0; 
    echo("<table border=1><tr>");
    foreach($arr as $a) 

    if ($i%3==0&&$i>0) echo "</tr><tr>"; 
    echo " <td>$a</td>"; 
    $i++; 
    }
    if ($i%3>0) echo str_repeat("<td>&nbsp;</td>",3-$i%3);
    echo("</tr></table>");