格式化循环php的结果

格式化循环php的结果,php,Php,我一直在制作一个包含4列的表,表的数据来自一个数组,这是我做的代码: $sample // this is an array and it has 15 values inside <table> foreach($sample as $x){ $rows="<td>$x</td>".$rows; $l++; if($l==4){ echo"<tr>".$rows."</tr>"; $l=0; $rows=""; } } </ta

我一直在制作一个包含4列的表,表的数据来自一个数组,这是我做的代码:

$sample // this is an array and it has 15 values inside
<table>
foreach($sample as $x){
$rows="<td>$x</td>".$rows;
$l++;
if($l==4){
echo"<tr>".$rows."</tr>";
$l=0;
$rows="";
}
}
</table>
1是$sample的数组值,而0没有值,因为og$sample的值仅为15

<?php
    echo "<table border='1'>";
    $sample = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15");          #with 15 rows
    echo "<tr>";
    $key   = 1; 
    foreach($sample as $each){
        if( ($key%4) == 0){
            echo "<td>{$each}</td></tr><tr>";
        }else{
            echo "<td>{$each}</td>";
        }
        $key++;
    }
    echo "</table>";
?>

请帮每个人(包括你自己)一个大忙,开始缩进你的代码。另外,我很确定上面的代码要么会导致语法错误,要么会输出PHP代码。3*4!=15... 除此之外,对所有事情都采取了非常错误的方法。我相信这是正确的:您确定
tr
s将正确关闭吗?是的,代码在我的本地主机上进行了测试,为什么会被否决@PeeHaaMy的评论应该是一个提示。。。
<?php
    echo "<table border='1'>";
    $sample = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15");          #with 15 rows
    echo "<tr>";
    $key   = 1; 
    foreach($sample as $each){
        if( ($key%4) == 0){
            echo "<td>{$each}</td></tr><tr>";
        }else{
            echo "<td>{$each}</td>";
        }
        $key++;
    }
    echo "</table>";
?>