从关联数组创建 HTML table
Make HTML table from Associative Array
我排序的数组在 var_dump 时看起来像这样。
array (size=11)
'The Matrix' => float 9.5
'Fight Club' => float 9.5
'Inception' => float 8.5
'The Usual Suspects' => float 7.5
'Shutter Island' => float 7.5
'The Prestige' => float 7
'The Dark Knight' => float 7
'The Departed' => float 6
'Matchstick Men' => float 5.5
'The Green Mile' => float 5
'Forrest Gump' => float 4.5
数组正在使用数据库中电影的标题和评分,因此当数据库中的电影或评分为 added/changed 时,数组中的数据将会更改。我的问题是:
如何从这个数组中生成 HTML table?
好了:
echo '<table>';
echo '<tr>
<th>Movie</th>
<th>Title</th>
</tr>';
foreach ($arr as $key=>$val) {
echo '<tr>
<td>'.$key.'</td>
<td>'.$val.'</td>
</tr>';
}
echo '</table>';
我排序的数组在 var_dump 时看起来像这样。
array (size=11)
'The Matrix' => float 9.5
'Fight Club' => float 9.5
'Inception' => float 8.5
'The Usual Suspects' => float 7.5
'Shutter Island' => float 7.5
'The Prestige' => float 7
'The Dark Knight' => float 7
'The Departed' => float 6
'Matchstick Men' => float 5.5
'The Green Mile' => float 5
'Forrest Gump' => float 4.5
数组正在使用数据库中电影的标题和评分,因此当数据库中的电影或评分为 added/changed 时,数组中的数据将会更改。我的问题是:
如何从这个数组中生成 HTML table?
好了:
echo '<table>';
echo '<tr>
<th>Movie</th>
<th>Title</th>
</tr>';
foreach ($arr as $key=>$val) {
echo '<tr>
<td>'.$key.'</td>
<td>'.$val.'</td>
</tr>';
}
echo '</table>';