我有一个数组,我正在尝试将信息输出到4个单元格宽的表中。表格的格式很好,但我无法回显数组信息。有谁知道怎么做?
Array看起来像这样:
[1] => Array
(
[photo_id] => 76
[photo_name] => 1284d830b198f21ee851334ab87140e1
)
[2] => Array
(
[photo_id] => 75
[photo_name] => 6ef145dd3f52e807f3d7afb3810a2ef8
)
[3] => Array
(
[photo_id] => 74
[photo_name] => f116f3a778d5e900a9e4f7b60c530b67
)
[4] => Array
(
[photo_id] => 73
[photo_name] => 87b2091970c85254391f1245f1826604
)
[5] => Array
(
[photo_id] => 72
[photo_name] => 549c75f04964d219974d4ceffa039e89
)
)
PHP代码:
echo "<table border=\"1\">\n";
for ($counter=0; $counter<$results; ) {
echo "<tr>\n";
for ($colcount=0; $colcount<4 AND $counter<$results; $colcount++,$counter++) {
echo "<td>1</td>";
}
if ($counter == $results) {
for ($extra = ($results % 4); $extra<4 AND $extra!=0; $extra++) {
echo "<td>0</td>";
}
}
echo "\n</tr>\n";
}
echo "</table>";
答案 0 :(得分:0)
我只看到三个细胞但是你可以这样做。
<?php
$test = array(1 => array('photo_id' => '76', 'photo_name' => '1284d830b198f21ee851334ab87140e1'),
'2' => array('photo_id' => '75', 'photo_name' => '6ef145dd3f52e807f3d7afb3810a2ef8'),
'3' => array('photo_id' => '74', 'photo_name' => 'f116f3a778d5e900a9e4f7b60c530b67'),
4 => array('photo_id' => '73', 'photo_name' => '1284d830b198f21ee851334ab87140e1'),
'5' => array('photo_id' => '72', 'photo_name' => '6ef145dd3f52e807f3d7afb3810a2ef8'),
'6' => array('photo_id' => '71', 'photo_name' => 'f116f3a778d5e900a9e4f7b60c530b67'),
7 => array('photo_id' => '70', 'photo_name' => '1284d830b198f21ee851334ab87140e1'),
'8' => array('photo_id' => '69', 'photo_name' => '6ef145dd3f52e807f3d7afb3810a2ef8'),
'9' => array('photo_id' => '68', 'photo_name' => 'f116f3a778d5e900a9e4f7b60c530b67')
);
echo '<table border="1">' . "\n";
echo '<tr><td>ID</td><td>PhotoId</td><td>Location/name</td></tr>' . "\n";
$count = 0;
foreach($test as $id => $photoinfo) {
foreach($photoinfo as $key => $value) {
if ($count % 4 == 0 && !empty($pid)) {
echo '<tr>';
}
if($key == 'photo_id') {
$pid = $value;
}
if ($key == 'photo_name') {
$name = $value;
}
if(!empty($pid) && !empty($name)) {
echo "<td>" . $pid . " " . $name . "</td>\n";
$count++;
if ($count % 4 == 0) {
echo '</tr>';
}
unset($name, $pid);
}
}
}
if ($count % 4 != 0) {
//we finished the table generation with less than 4 cells
while($count % 4 != 0) {
echo '<td>empty cell</td>';
$count++;
}
echo '</tr>';
}
echo "</table>\n";
输出:
<table border="1">
<tr><td>ID</td><td>PhotoId</td><td>Location/name</td></tr>
<tr><td>76 1284d830b198f21ee851334ab87140e1</td>
<td>75 6ef145dd3f52e807f3d7afb3810a2ef8</td>
<td>74 f116f3a778d5e900a9e4f7b60c530b67</td>
<td>73 1284d830b198f21ee851334ab87140e1</td>
</tr><tr><td>72 6ef145dd3f52e807f3d7afb3810a2ef8</td>
<td>71 f116f3a778d5e900a9e4f7b60c530b67</td>
<td>70 1284d830b198f21ee851334ab87140e1</td>
<td>69 6ef145dd3f52e807f3d7afb3810a2ef8</td>
</tr><tr><td>68 f116f3a778d5e900a9e4f7b60c530b67</td>
<td>empty cell</td><td>empty cell</td><td>empty cell</td></tr></table>