2014-05-21 16 views
1

我有一個動態數組:如何以示例的方式將數組更改爲html表格?

Array 
(
    [user1] => Array 
     (
      [012014] => 6788 
      [022014] => 11141 
      [032014] => 6143 
      [042014] => 936 
      [052014] => 936 
     ) 

    [user2] => Array 
     (
      [012014] => 9 
      [022014] => 25 
      [032014] => 37 
      [042014] => 17 
      [052014] => 16 
     ) 

) 

我想是這樣顯示的:我好像

Users |    Months 
     |--------------------------------- 
     |012014|022014|032014|042014|052014 
------------------------------------------ 
user1 | 6788 | 11141| 6143 | 936 | 936 
------------------------------------------- 
user2 | 9 | 25 | 37 | 17 | 16 

不能做得出來!這是我一直在嘗試:

echo "<table>"; 

foreach($month_all as $site=>$value){ 
    echo "<tr>"; 
    echo "<td>$site</td>"; 
    foreach ($value as $column) { 
     echo "<td>$column</td>"; 
    } 
    echo "</tr>"; 
}  
echo "</table>"; 

任何方式,我可以做到這一點乾淨的方式?

+0

您需要2'foreach'循環。 –

回答

1

使用兩個循環:一個用於顯示所有鍵值。一個用於顯示用戶的所有值。此版本使用<th>來處理表格標題並正確處理縮進,因爲它使用替代foreach樣式。

<table> 
    <tr> 
     <th>User</th> 
     <th colspan="5">Months</th> 
     <tr> 
      <th></th> 
      <?php foreach ($month_all['user1'] as $key): ?> 
       <td><?= $key ?></td> 
      <?php endforeach ?> 
     </tr> 
    </tr> 
    <?php foreach ($month_all as $site => $value): ?> 
     <tr> 
      <td><?= $site ?></td> 
      <?php foreach ($value as $column): ?> 
       <td><?= $column ?></td> 
      <?php endforeach ?> 
     </tr> 
    <?php endforeach ?> 
</table> 

Live demo

1

你可以使用這樣

<table> 
<tr> 
    <?php 
     foreach($Arr['user1'] as $key1=>$th) 
     { 
      ?> 
      <th><?=$key1?></th> 
      <?php 
     } 
    ?> 
</tr> 
<?php 
foreach($Arr as $row) 
{ ?> 
    <tr> 
     <?php 
     foreach($row as $column){ 
     ?> 
     <td><?=$column?></td> 
     <?php } ?> 
    </tr> 
<?php} 
?> 
</table> 
1

這將要給你確切的表作爲你的問題

<?php 
$array = array(
    "user1" => array(
     "012014" => 6788, 
     "022014" => 11141, 
     "032014" => 6143, 
     "042014" => 936, 
     "052014" => 936 
    ) , 

    "user2" => array(
     "012014" => 9, 
     "022014" => 25, 
     "032014" => 37, 
     "042014" => 17, 
     "052014" => 16 
    ) 
); 

print '<table border="1px solid black"><tr><td>Users</td><td colspan="5" style="text-align: center">Month</td></tr>'; 
print '<tr><td></td>'; 
foreach (reset($array) as $key => $value) { 
    print '<td>' . $key . '</td>'; 
} 
print '</tr>'; 
foreach ($array as $key => $values) { 
    print '<tr>'; 
    print '<td>' . $key . '</td>'; 
    foreach ($values as $value) { 
     print '<td>' . $value . '</td>'; 
    } 
    print '</tr>'; 
} 
print '</table>'; 
?> 

結果:

1

使用破滅是不是一個好講清楚方法來使用。但是我想給你一個想法,你應該在header中使用colspan='.count(array_keys($month_all['user1'])).'來獲得所有列的月份。

echo '<table><tr><td rowspan=2>Users</td><td colspan='.count(array_keys($month_all['user1'])).'>Months</td></tr>'; 
echo '<td>'.rtrim(implode(array_keys($month_all['user1']),'</td><td>'),'<td>').''; 
foreach($month_all as $site=>$value){ 
    echo "<tr>"; 
    echo "<td>$site</td>"; 
    foreach ($value as $column) { 
     echo "<td>$column</td>"; 
    } 
    echo "</tr>"; 
} 
相關問題