Reputation: 972
I have a dynamic 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
)
)
And I want to display it like this :
Users | Months
|---------------------------------
|012014|022014|032014|042014|052014
------------------------------------------
user1 | 6788 | 11141| 6143 | 936 | 936
-------------------------------------------
user2 | 9 | 25 | 37 | 17 | 16
I can't seem to work it out! Here's what I've been trying :
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>";
Any way I can do it in a clean way ?
Upvotes: 1
Views: 107
Reputation: 2607
This will give u the exact table as in your question
<?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>';
?>
result:
Upvotes: 1
Reputation: 76646
Use two loops: one for displaying all the key values. One for displaying all the values of the users. This version uses <th>
to handle the table headers and takes care of the indentation properly because it's using the alternative foreach
style.
<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>
Upvotes: 1
Reputation: 760
Clearly speaking using implode is not a good method to use.But i wanted to give you an idea that you should use colspan='.count(array_keys($month_all['user1'])).'
in header to get month over all columns.
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>";
}
Upvotes: 1
Reputation: 12039
you can use like this
<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>
Upvotes: 1