如何在键是字符串时打印多维数组?

时间:2013-03-14 04:33:54

标签: php arrays multidimensional-array

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

我有建议使用提取方法。但是没弄清楚。希望有人能给我一些线索。

谢谢,

3 个答案:

答案 0 :(得分: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>';

答案 1 :(得分:0)

试试这个:

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>';

答案 2 :(得分:0)

试试这个:

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