日期:2014-05-17  浏览次数:20443 次

如何循环输出含有tr的td
eg:$arr1=array("a","b","c","d","e","f","g"); 
循环上面的。

得到如下效果
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td>e</td>
<td>f</td>
<td>g</td>
<td></td>
</tr>

以此类推,请问如何用PHP写出来

------解决方案--------------------
PHP code
foreach(array_chunk($arr1,4) as $v){
      echo "<tr>\n";
    foreach($v as $t) echo "<td>".$t."</td>\n";
    echo "</tr>\n";
};

------解决方案--------------------
$arr1=array("a","b","c","d","e","f","g");

$i = 1;
echo '<tr>';
foreach ($arr1 as $key=>$val)
{
echo '<td>' , $val, '</td>';
if($i%4 == 0) echo "</tr><tr>";
++$i;
}
echo '</tr>';