kiki kiki - 4 months ago 10
PHP Question

single php array to html table

what is the idea to print html table from this array

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


i expect my table to be something like

1 2 3

4 5 6

7 8 9


i tried a lot but i couldn't find an idea to do this

my idea were to break each 3 element but i need something more smart

Answer

You can use array-chunk like this:

$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>";
Comments