user2923349 user2923349 - 1 month ago 17
PHP Question

Printing arrays as tables

I have a multidimensional array where each key holds another array as value. I printed each key value pairs in separate tables. But I get unwanted space between tables (spacing between any two tables are not the same). How can I eliminate spacing as a whole?

foreach ($stockist as $key => $value)
{
echo "<table align='center' border='1'>";
echo "<tr><td align = 'center'> <font color = 'blue'> $key</td></tr>";
foreach($value as $key1 => $value1)
{
echo "<tr><td align ='center'>$value1</td></tr><br>";
}
echo "</table>";
}

Answer

This should get it all into a single table.

PHP

echo '<table>';
foreach ($stockist as $key => $value) 
{
    echo '<tr><td>'.$key.'</td></tr>';
    foreach($value as $key1 => $value1)
    {
        echo '<tr><td>'.$value1.'</td></tr>';
    }
}
echo '</table>;