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

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条)

相关推荐 更多相似问题