アイデアは、この配列からHTMLテーブルを印刷することである。シングルPHPの配列
$arr = ['1','2','3','4,'5,'6','7','8','9'];
私は私のテーブルのようなものであることを期待:
1 2 3
4 5 6
7 8 9
私は多くのことを試みたが、Iこれを行うアイデアを見つけることができませんでした。
私の考えは、3つの要素のそれぞれを壊すことでしたが、私はよりスマートなものが必要です。
アイデアは、この配列からHTMLテーブルを印刷することである。シングルPHPの配列
$arr = ['1','2','3','4,'5,'6','7','8','9'];
私は私のテーブルのようなものであることを期待:
1 2 3
4 5 6
7 8 9
私は多くのことを試みたが、Iこれを行うアイデアを見つけることができませんでした。
私の考えは、3つの要素のそれぞれを壊すことでしたが、私はよりスマートなものが必要です。
あなたはこのよう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