从关联数组创建HTML表

时间:2015-12-06 15:53:27

标签: html arrays html-table associative

我在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

数组使用来自数据库的电影的标题和评级,因此当在数据库中添加/更改电影或评级时,数组中的数据将会更改。我的问题是:

如何从这个数组制作HTML表格?

1 个答案:

答案 0 :(得分:0)

你去了:

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>';
相关问题