仅使用值数组中的PHP创建表

I've to print a table using TCPDF for creating a PDF using PHP from an array of contents.

In my example I'll use 7 entries, but can be more or less.

$array = array(
 0 => "[ ] Option 1",
 1 => "[x] Option 2",
 2 => "[ ] Option 3",
 3 => "[ ] Option 4",
 4 => "[x] Option 5",
 5 => "[ ] Option 6",
 6 => "[ ] Option 7",
 ...
);

My table layout is dynamic: I can set numbers of columns for display purpose ( $num_cols can be 1, 2, 3 or 4 columns)

Based on my $num_cols, I've to split my content of the array in each TD cell.

So, if I set $num_cols = 4, i need to show something like that:

<table>
  <tr>
    <td>[ ] Option 1</td>
    <td>[x] Option 2</td>
    <td>[ ] Option 3</td>
    <td>[ ] Option 4</td>
  </tr>
  <tr>
    <td>[x] Option 5</td>
    <td>[ ] Option 6</td>
    <td>[ ] Option 7</td>
    <td></td>
  </tr>
</table>

I tried to start some ideas but in this example I can get only a row. I don't know how to create the external loop:

echo "<table>";
echo "<tr>";

for ( $i = 0; $i < $num_cols; $i++ ) {

 echo "<td>";
 if ( isset($array[$i]) {echo $array[$i];} else {}
 echo "</td>";

}
echo "</tr>";
echo "</table>";

array-chunk will help you:

<?php
$num_cols = 4;
$array = [1,2,3,4,5,6,7,8];

echo "<table>";
foreach (array_chunk($array, $num_cols) as $chunk) {
    echo "<tr>";
    foreach ($chunk as $item) {
        echo "<td>";
        echo $item;
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";

and you can use some other cool-stuff from php :)

<?php
$num_cols = 4;
$array = [1,2,3,4,5,6,7,8];

echo "<table><tr><td>" .
    implode("</td></tr><tr><td>",
        array_map(function(array $part) {
            return implode('</td><td>', $part);
        }, array_chunk($array, $num_cols))
    )
. "</td></tr></table>";
<?php
$array = array(
 0 => "[ ] Option 1",
 1 => "[x] Option 2",
 2 => "[ ] Option 3",
 3 => "[ ] Option 4",
 4 => "[x] Option 5",
 5 => "[ ] Option 6",
 6 => "[ ] Option 7"
);
$num_cols = 4;

//Use $i as a counter
$i = 0;
echo '<table>';
foreach ($array as $key => $val) {
    //If we are outputting the first column in a row, create a new tr tag
    if ($i % $num_cols == 0)
        echo '<tr>';

    //Output the column
    echo '<td>' . $val . '</td>';

    //If we are at the end of the number of columns, close the tr tag
    if ($i % $num_cols == 3)
        echo '</tr>';
    $i++;
}

//If we didn't close the tr tag in the loop, close it now
if ($i % $num_cols != 0)
    echo '</tr>';
echo '</table>';
?>