Display 2D Array as HTML Table in PHP
PHP How ToYou can display a 2 dimensional array in PHP as a HTML table using nested foreach loop. For example, this is useful when you get rows of information from MySQL, and then you need to display its output as a table.
The function uses two foreach loops. The first or outer loop gets one row at a time. The second loop or inner loop gets columns from the row as table data.
public function displayTable()
{
$disp = '<table class="table">';
foreach ($this->rows as $tr)
{
$disp .= '<tr>';
foreach ($tr as $td)
{
$disp .= '<td>' . $td . '</td>';
}
$disp .= '</tr>';
}
$disp .= '</table>';
echo $disp;
}