我有一个这样的数组:
Array
(
[0] => Array
(
[date] => 2012-11-29
[description] => Work Done
[hours] => 1.0
[id] => 6502
[person-id] => 8853
[project-id] => 8928
[todo-item-id] => 122353
[person-name] => Jane Doe
)
[1] => Array
(
[date] => 2012-11-29
[description] => Work Done 2
[hours] => 1.0
[id] => 6502
[person-id] => 8853
[project-id] => 8928
[todo-item-id] => 122353
[person-name] => Jane Doe
)
[2] => Array
(
[date] => 2012-11-28
[description] => Work Done 3
[hours] => 1.0
[id] => 6502
[person-id] => 8853
[project-id] => 8928
[todo-item-id] => 122353
[person-name] => Jane Doe
)
等
我想在按日期分组的表格中显示此信息。我正在使用foreach来获取每个值但我正在试图弄清楚如何检查每个数组中的日期是否相同,如果是这样的话只回显日期一次。这是我到目前为止所得到的......
<table>
<tbody>
<?php
foreach($time as $log){
//tried this but it doesn't work
//if(array_unique($log['date'])){
echo '<thead>
<tr class="info">
<td colspan="3">'.$log[date].'</td>
</tr>
</thead>';
//};
echo "<tr>";
echo "<td>{$log['todo-item-id']}</td>";
echo "<td>{$log['description']}</td>";
echo "<td>{$log['hours']}</td>";
echo "</tr>";
} ?>
</tbody>
</table>
答案 0 :(得分:3)
你只能有一个thead。在循环结束时或在完成以下比较后设置日期:此日期不等于前一个日期,或未设置。如果是这样,输出它会输出一个不间断的空间。
<?php
foreach($time as $log) {
// check if it is not set or not equal and output if needed
echo '<tr class="info">
<td colspan="3">'.(!isset($prevdate) || $prevdate!=$log[date]? $log[date] : ' ').'</td>
</tr>
';
echo "<tr>
<td>{$log['todo-item-id']}</td>
<td>{$log['description']}</td>
<td>{$log['hours']}</td>
</tr>";
// set it
$prevdate=$log[date];
}
?>
答案 1 :(得分:0)
当您浏览循环时,保存上一个日期,如果它相同则不回显它,否则将新日期保存为上一个日期并回显新日期。
答案 2 :(得分:0)
作为您问题的解决方案,请参阅以下代码段。
<table>
<tbody>
<?php
$a=array();
foreach($time as $log){
if(!in_array($log['date'],$a)) { array_push($a,$log['date']);
echo '<thead>
<tr class="info">
<td colspan="3">'.$log[date].'</td>
</tr>
</thead>';
}
echo "<tr>";
echo "<td>{$log['todo-item-id']}</td>";
echo "<td>{$log['description']}</td>";
echo "<td>{$log['hours']}</td>";
echo "</tr>";
} ?>
</tbody>
</table>