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>

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

That should work:

<html>
<body>
<table>
    <?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 "<th>" . $key . "</th>"; //use "th" instead for the header
    }
    echo "</tr>";

    foreach($karma_score as $key=> $value){
        echo "<tr>"; //a row for each key
        foreach($value as $something){ //foreach row print all the columns
            echo "<td>";
            echo $something;
            echo "</td>";
        }
        echo "</tr>";
    }

    ?>
</table>
</body>
</html>