kiki kiki - 1 year ago 70
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 Source

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>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download