<?php function getTableHtml(array $data, int $colLength): string { $result = ''; $rowNumber = 0; $rows = []; foreach (array_values($data) as $k => $v) { if ($k % $colLength === 0) { ++$rowNumber; $rows[$rowNumber] = ''; } $rows[$rowNumber] .= '<td>'; $rows[$rowNumber] .= htmlentities($v); $rows[$rowNumber] .= '</td>'; $rows[$rowNumber] .= PHP_EOL; // optional formatting } $result = '<table><tr>' . join('</tr><tr>', $rows) . '</tr></table>'; return $result; } $array = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]; echo getTableHtml($array, 4), PHP_EOL;
You have javascript disabled. You will not be able to edit any code.