在块列表/表中显示数组元素


display array elements in block list / table

我有一个计数数字数组。我想将它们显示在具有特定列数的表或块列表中,例如使用 php 的 foreachecho 函数显示 6 列,以便我可以循环运行。我如何使用csshtml做到这一点?

这就是我在说的

+-----+-----+-----+-----+-----+-----+
|  1  |  2  |  3  |  4  |  5  |  6  |
+-----+-----+-----+-----+-----+-----+
|  7  |  8  |  9  |  10 |  11 |  12 |
+-----+-----+-----+-----+-----+-----+
|  13 |  14 |  15 |  16 |  17 |  18 |
+-----+-----+-----+-----+-----+-----+
|  19 |  20 |  21 |  22 |  23 |  24 |
+-----+-----+-----+-----+-----+-----+

如果通过设置<table>来完成会更好

 $elements = array_chunk($elements, 6);
 $html = '<table>';
 foreach($elements as $tr){
     $html .= '<tr>';
     foreach($tr as $td) $html .= '<td>'.$td.'</td>';
     $html .= '</tr>';
 }
 $html .= '</table>;

试试这个

<table>
    <tr>
        <?php
        $columns = 6;
        $i = 0;
        foreach($array as $item)
        {
            echo "<td>$item<td>";
            if($i++ >= $columns)
            {
                $i = 0;
                echo "</tr><tr>";
            }
        }
        ?>
     </tr>
</table>