1 回答

TA貢獻1842條經驗 獲得超22個贊
在 $testing 中已經有了這個結構:
Array ( [Knight ] => Array ( [0] => Array ( [name] => Dave ) [1] => Array ( [name] => Simon ) ) [Lieutenant ] => Array ( [0] => Array ( [name] => Tom ) ) )
做表
<?php
// Just to keep same variable
$output = $testing;
?>
<table border=1>
<thead>
<tr>
<?php
// We need to know wich $title has more names
$max = 0;
foreach($output as $title => $names) {
if(count($names) > $max) {
$max = count($names);
}
?>
<th><?php echo $title;?></th>
<?php } // end foreach ?>
</tr>
</thead>
<tbody>
<?php
// Loop for creating table rows
for($i = 0; $i < $max; $i++) {
?>
<tr>
<?php
// Loop for creating table cells
foreach($output as $title => $names) {
?>
<td><?php
// Echo only if name exists
if(isset($names[$i]['name'])) {
echo $names[$i]['name'];
}
?></td>
<?php
} // end foreach
?>
</tr>
<?php
} // end for
?>
</tbody>
</table>
- 1 回答
- 0 關注
- 163 瀏覽
添加回答
舉報