douyi6922 2015-02-25 23:59
浏览 50
已采纳

PHP - 将多维数组输出到HTML表中的问题

I'm attempting to write a php code to use sample information in a multidimensional array, and output that info into an html table.

I am able to output the information but the formatting is way off, I feel as if there is some small issue and I need an extra pair of eyes.

Any help or advice is greatly appreciated.

My PHP code:

<html>

<head>
</head>

<body>

<table border="1px">

<?php

$karma_score = Array( "UserID" => Array(1,2,3,4),
                      "NameID" => Array('Doe','Smith','Chan','Zee'),
                      "Karma" => Array(45,123,1,15),
                      "LastLogin" => Array("2012-08-30","2012-09-02","2011-12-23","2012-07-01"));

    echo "<tr>";
foreach($karma_score as $key => $value){
    echo "<td>" . $key . "</td>";
}
echo "</tr>";

echo "<tr>";
foreach($karma_score as $key=> $value){
    echo "<td>";
    foreach($value as $something){
        echo $something;
    }
    echo "</td>";
}
echo "</tr>";



?>

</table>

</body>

</html>
  • 写回答

2条回答 默认 最新

  • dqhsv0147421 2015-02-26 00:14
    关注

    in my comment, or the other way around

    <html>
    
    <head>
    </head>
    
    <body>
    
    <table border="1px">
    
    <?php
    
    $karma_score = Array( "UserID" => Array(1,2,3,4),
                          "NameID" => Array('Doe','Smith','Chan','Zee'),
                          "Karma" => Array(45,123,1,15),
                          "LastLogin" => Array("2012-08-30","2012-09-02","2011-12-23","2012-07-01"));
    
    echo '<tr>';
    foreach(array_keys($karma_score) as $head){
    echo '<th>'.$head.'</th>';
    }
    echo '</tr>';
    foreach($karma_score['UserID'] as $key=> $value){
        echo "<tr>";
    
    
            echo '<td>'.$karma_score['UserID'][$key].'</td>';
            echo '<td>'.$karma_score['NameID'][$key].'</td>';
            echo '<td>'.$karma_score['Karma'][$key].'</td>';
            echo '<td>'.$karma_score['LastLogin'][$key].'</td>';
    
    
    echo "</tr>";
    }
    
    
    
    
    ?>
    
    </table>
    
    </body>
    
    </html>
    

    demo: http://codepad.viper-7.com/ZFj6gb

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号