如何打印多维数组时,它的键是一个字符串


Array ( 
    [Team1] => Array ( 
        [win] => 1 
        [draw] => 0 
        [lost] => 0 
    ) 
    [Team2] => Array ( 
        [win] => 1 
        [draw] => 1 
        [lost] => 1 
    ) 
    [Team3] => Array ( 
        [win] => 1 
        [draw] => 0 
        [lost] => 1 
    ) 
    [AnotherTeam] => Array ( 
        [win] => 0 
        [draw] => 0 
        [lost] => 1 
    ) 
    [LastTeam] => Array ( 
        [win] => 0 
        [draw] => 1 
        [lost] => 0 
    ) 
)
上面是一个生成数组的例子。想象一下,有一些足球俱乐部,他们赢了,平了,输了一些比赛。

我想将这个数组打印为表的行和列,如下所示;

Team   |  Win |  Draw  |  Lost
Team 1    1      0        0
Team 2    1      1        1

我有一个建议使用提取方法。但没搞清楚。希望有人能给我点提示。

谢谢,

$teamArr=Array ('Team1' => Array ('win' => 1 ,'draw' => 0,'lost' => 0),
'Team2' => Array ('win' => 1 ,'draw' => 0,'lost' => 0) ,
'Team3' => Array ('win' => 1 ,'draw' => 0,'lost' => 0),
'Another team' => Array ('win' => 1 ,'draw' => 0,'lost' => 0),
'Last Team' => Array ('win' => 1 ,'draw' => 0,'lost' => 0));
echo '<table width=100%><tr><th>Teams</th><th>Win</th><th>Draw</th><th>Lost</th> </tr>';
foreach($teamArr as $key => $value) {
 echo "<tr> <th>$key</th>";
        foreach($value as $status) {
          echo "<th>$status</th>";
        }
 echo "</tr>";
}
echo '</table>';

试试这个:

echo '<table><tr><th>Teams</th><th>Win</th><th>Draw</th><th>Lost</th></tr>';
foreach($teamsArray as $key => $value) {
 echo "<tr> <td>$key</td>";
            foreach($value as $status) {
              echo "<td>$status</td>";
            }
 echo "</tr>";
}
echo '</table>';

试试这个:

<table>
<tr><td>Team</td><td>Win</td><td>Draw</td><td>Lost</td></tr>
<?php foreach($your_array as $key=>$val){ ?>
<tr><td><?php echo $key;?></td><td><?php echo $val['win'];?></td><td><?php echo $val['draw'];?></td><td><?php echo $val['lost'];?></td></tr>
<?php } ?>
</table>

相关内容

  • 没有找到相关文章

最新更新