doudi8829 2014-05-21 18:20
浏览 10
已采纳

如何以样本方式将数组更改为html表?

I have a dynamic array :

Array
(
    [user1] => Array
        (
            [012014] => 6788
            [022014] => 11141
            [032014] => 6143
            [042014] => 936
            [052014] => 936
        )

    [user2] => Array
        (
            [012014] => 9
            [022014] => 25
            [032014] => 37
            [042014] => 17
            [052014] => 16
        )

)

And I want to display it like this :

Users |              Months
      |---------------------------------
      |012014|022014|032014|042014|052014
------------------------------------------
user1 | 6788 | 11141| 6143 |  936 |  936
-------------------------------------------
user2 |   9  |   25 |   37 |   17 |  16 

I can't seem to work it out! Here's what I've been trying :

echo "<table>";

foreach($month_all as $site=>$value){
   echo "<tr>";
   echo "<td>$site</td>";
   foreach ($value as $column) {
      echo "<td>$column</td>";
   }
   echo "</tr>";
}    
echo "</table>";

Any way I can do it in a clean way ?

  • 写回答

4条回答 默认 最新

  • douyingbei1458 2014-05-21 18:58
    关注

    Use two loops: one for displaying all the key values. One for displaying all the values of the users. This version uses <th> to handle the table headers and takes care of the indentation properly because it's using the alternative foreach style.

    <table>
        <tr>
            <th>User</th>
            <th colspan="5">Months</th>
            <tr>
                <th></th>
                <?php foreach ($month_all['user1'] as $key): ?>
                    <td><?= $key ?></td>
                <?php endforeach ?>
            </tr>
        </tr>
        <?php foreach ($month_all as $site => $value): ?>
            <tr>
                <td><?= $site ?></td>
                <?php foreach ($value as $column): ?>
                    <td><?= $column ?></td>
                <?php endforeach ?>
            </tr>
        <?php endforeach ?> 
    </table>
    

    Live demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分