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

Above is an example of a generated array. Imagine there are some football clubs with their win draw and lost number of games.

I would like to print this array as rows and columns of a table like following;

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

I had an advice to use extract method. But didn't figure out well. Hope anyone can give me a clue.

Thanks,

1
  • 1
    array_keys($yourArray[0]) should give you the array of header. Just iterate over this array to print the header. Finally iterate of the initial array $yourArray to print the content. Commented Mar 14, 2013 at 4:40

3 Answers 3

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>';
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

1 Comment

Initially $teamArray has the set of all values. With that key value, it creates a new row of record like [Team1] and $value also contains another array. For that , loop the status of table data values.
0

Try this :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.