将2D php数组转换为HTML表

时间:2011-09-24 09:40:05

标签: php html arrays html-table

如何通过PHP数组实现,在html表中得到以下结果:

Server1          db_1

Server1          db_2

Server1          db_3

Server2          db_1

Server2          db_2

Server3          db_3

1 个答案:

答案 0 :(得分:2)

喜欢这个吗?

$servers = array(
    'Server 1' => array(
        'db_1',
        'db_2',
        'db_3'
    ),
    'Server 2' => array(
        'db_1',
        'db_2'
    ),
    'Server 3' => array(
        'db_3'
    )
);
echo '<table>';
foreach($servers as $server=>$dbs)
{
    foreach($dbs as $db)
    {
        echo '<tr><td>'.$server.'</td><td>'.$db.'</td></tr>';
    }
}
echo '</table>';