Php 从数组中的值生成4列表

Php 从数组中的值生成4列表,php,html,arrays,html-table,Php,Html,Arrays,Html Table,您得到了数组: $tab=[1,2,3,4,5,6,7,8,9] 我想用数组选项卡中的值创建表 表格最多只能有4列。 它应该是这样的: 1 2 3 4 5 6 7 8 9 我的示例代码(不工作): $tab=[1,2,3,4,5,6,7,8,9]; $max=计数($tab); //1行 回声“; 对于($i=0;$i

您得到了数组:
$tab=[1,2,3,4,5,6,7,8,9]

我想用数组选项卡中的值创建表
表格最多只能有4列。
它应该是这样的:

1 2 3 4
5 6 7 8
9
我的示例代码(不工作):

$tab=[1,2,3,4,5,6,7,8,9];
$max=计数($tab);
//1行
回声“;
对于($i=0;$i<$max;$i+=4)
{
回声“;
//2个FOR-COLUMNS
对于($x=$i;$x<4;$x++)//我们的数组是9元素的,在第三行,$x将超出索引
{
printf('%s',$tab[$x]);
}
回声“;
}
回声“;
$arr=[1,2,3,4,5,6,7,8,9];
$chunks=array\u chunk($arr,4);
回声';
foreach($chunk作为$chunk){
回声';
foreach($chunk作为$val){
printf('%s',$val);
}
回声';
}
回声';
$arr=[1,2,3,4,5,6,7,8,9];
$chunks=array\u chunk($arr,4);
回声';
foreach($chunk作为$chunk){
回声';
foreach($chunk作为$val){
printf('%s',$val);
}
回声';
}
回声';
$tab=数组(1,2,3,4,5,6,7,8,9);
$max=计数($tab);
回声“;
回声“;
对于($i=0;$i<$max;$i++){
如果($i>0&&$i%4==0){
回声“;
}
printf('%s',$tab[$i]);
}
回声“;
回声“;
$tab=数组(1,2,3,4,5,6,7,8,9);
$max=计数($tab);
回声“;
回声“;
对于($i=0;$i<$max;$i++){
如果($i>0&&$i%4==0){
回声“;
}
printf('%s',$tab[$i]);
}
回声“;
回声“;


$tab=[1,2,3,4,5,6,7,8,9];
$max=计数($tab);
回声“;
对于($i=0;$i
$tab=[1,2,3,4,5,6,7,8,9];
$max=计数($tab);
回声“;

对于($i=0;$iForgot完全关于
array\u chunk
。好吧,我正式对PHP生疏了。使用
array\u chunk
的代码更漂亮,但速度较慢,因此使用
mod
操作符的解决方案完全正确,
foreach
for
;)我认为解决方案并不容易(不知道函数)。这就是我想要的。非常感谢!完全忘记了
array\u chunk
。好吧,我正式对PHP生疏了。使用
array\u chunk
代码更漂亮,但速度较慢,所以使用
mod
操作符的解决方案完全正确,
foreach
for
更好。)我认为解决方案不容易(不知道函数)。这就是我想要的。非常感谢!
$tab = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$max = count($tab);

// 1 FOR - ROWS
echo "<table>";
    for ( $i = 0; $i < $max; $i+=4 )
    {
    echo "<tr>";
        // 2 FOR - COLUMNS
        for ( $x = $i; $x < 4; $x++ ) //our array is 9-elemented, in third row $x going to be out of index
        {
            printf('<td>%s</td>', $tab[$x]);
        }
    echo "</tr>";
    }
echo "</table>";
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$chunks = array_chunk($arr, 4);
echo '<table>';
foreach ($chunks as $chunk) {
    echo '<tr>';
    foreach ($chunk as $val) {
        printf('<td>%s</td>', $val);
    }
    echo '</tr>';
}
echo '</table>';
$tab = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$max = count($tab);

echo "<table>";
    echo "<tr>";
    for ( $i = 0; $i < $max; $i++ ) {
        if ($i > 0 && $i % 4 == 0) {
            echo "</tr><tr>";
        }
        printf('<td>%s</td>', $tab[$i]);
    }
    echo "</tr>";
echo "</table>";
<?php
    $tab = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    $i=1;
    echo '<table>'
    foreach($tab as $key => $val){
        if ($i==1) echo '<tr>'
        if($i%4==0){ $i=0; echo "<td>$val</td> </tr>";}
        else {
          echo "<td> $val </td>";
        }
        $i++
    }
    echo '</table>'
?>
$tab = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$max = count($tab);
echo "<table>";
    for($i=0;$i<$max;$i=$i+4){
        echo '<tr>';
            for($x=0;$x<4;$x++){
                if($x+$i>=$max){
                    continue;
                }else{
                    echo '<td>'.$tab[($x+$i)].'</td>';
                }
            }
        echo '</tr>';
    }
echo "</table>";