Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单个php数组到html表_Php_Html_Arrays_Html Table - Fatal编程技术网

单个php数组到html表

单个php数组到html表,php,html,arrays,html-table,Php,Html,Arrays,Html Table,其想法是从该数组打印一个html表: $arr = ['1','2','3','4,'5,'6','7','8','9']; 我希望我的桌子是这样的: 1 2 3 4 5 6 7 8 9 我试了很多,但我找不到这样做的想法 我的想法是打破每三个元素,但我需要更聪明的东西。 <?php $arr = ['1','2','3','4','5','6','7','8','9']; print "<table>\n"; foreach(array_chunk($arr, 3)

其想法是从该数组打印一个html表:

$arr = ['1','2','3','4,'5,'6','7','8','9'];
我希望我的桌子是这样的:

1 2 3

4 5 6

7 8 9
我试了很多,但我找不到这样做的想法

我的想法是打破每三个元素,但我需要更聪明的东西。


<?php
$arr = ['1','2','3','4','5','6','7','8','9'];
print "<table>\n";
foreach(array_chunk($arr, 3) as $row) {
        print "<tr>";
        foreach($row as $col) {
                print "<td>";
                print $col;
                print "</td>";
        }   
        print "</tr>\n";
}
print "</table>";
?>

您可以这样使用:

$arr = ['1','2','3','4','5','6','7','8','9'];

echo "<table>";
foreach(array_chunk($arr, 3) as $row) {
    echo "<tr>";
    foreach($row as $cell) {
        echo "<td>$cell</td>";   
    }
    echo "</tr>";
}
echo "</table>";
$arr=['1'、'2'、'3'、'4'、'5'、'6'、'7'、'8'、'9'];
回声“;
foreach(数组块($arr,3)作为$row){
回声“;
foreach($行作为$单元格){
回声“$cell”;
}
回声“;
}
回声“;
您可以这样使用:

$arr = ['1','2','3','4','5','6','7','8','9'];

echo "<table>";
foreach(array_chunk($arr, 3) as $row) {
    echo "<tr>";
    foreach($row as $cell) {
        echo "<td>$cell</td>";   
    }
    echo "</tr>";
}
echo "</table>";
$arr=['1'、'2'、'3'、'4'、'5'、'6'、'7'、'8'、'9'];
回声“;
foreach(数组块($arr,3)作为$row){
回声“;
foreach($行作为$单元格){
回声“$cell”;
}
回声“;
}
回声“;
$arr=['1'、'2'、'3'、'4'、'5'、'6'、'7'、'8'、'9'];
$from=0//来自arr的索引
$number=3//每行单元格数
回声“;
而($row=array\u slice($arr,$from,$number)){
回声“;
foreach($行作为$单元格){
回声“$cell”;
}
回声“;
$from+=$number;
}
回声“;
$arr=['1'、'2'、'3'、'4'、'5'、'6'、'7'、'8'、'9'];
$from=0//来自arr的索引
$number=3//每行单元格数
回声“;
而($row=array\u slice($arr,$from,$number)){
回声“;
foreach($行作为$单元格){
回声“$cell”;
}
回声“;
$from+=$number;
}
回声“;