的想法是从这个数组打印一个HTML表格:单一的PHP数组HTML表格
$arr = ['1','2','3','4,'5,'6','7','8','9'];
我希望我的表是这样的:
1 2 3
4 5 6
7 8 9
我尝试了很多,但我找不到一个想法来做到这一点。
我的想法是打破每三个元素,但我需要更聪明的东西。
的想法是从这个数组打印一个HTML表格:单一的PHP数组HTML表格
$arr = ['1','2','3','4,'5,'6','7','8','9'];
我希望我的表是这样的:
1 2 3
4 5 6
7 8 9
我尝试了很多,但我找不到一个想法来做到这一点。
我的想法是打破每三个元素,但我需要更聪明的东西。
您可以使用array-chunk
这样的:
$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>";
<?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'];
$from=0; //index from of arr
$number=3; //number cell per row
echo "<table border='1'>";
while($row=array_slice($arr,$from,$number)){
echo "<tr>";
foreach($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>";
$from+=$number;
}
echo "</table>";
非常感谢你 – kiki