我有一个历史列表,按日期DESC分组如下:
-----------
28/Feb/2014
-----------
27/Feb/2014
-----------
27/Feb/2014
-----------
27/Feb/2014
------------
26/Feb/2014
------------
26/Feb/2014
我希望像这样打印。
-----------
28/Feb/2014
-----------
27/Feb/2014
27/Feb/2014
27/Feb/2014
------------
26/Feb/2014
26/Feb/2014
这是我的数组:
array(
(int) 0 => array(
'BriefHistory' => array(
'id' => '2',
'created' => '2014-02-28 14:51:08',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '1'
)
),
(int) 1 => array(
'BriefHistory' => array(
'id' => '4',
'created' => '2014-02-27 16:18:40',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '2',
'brief_history_type_id' => '4'
)
),
(int) 2 => array(
'BriefHistory' => array(
'id' => '1',
'created' => '2014-02-27 14:51:08',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '1'
)
),
(int) 3 => array(
'BriefHistory' => array(
'id' => '3',
'created' => '2014-02-26 16:18:09',
'created_by' => '7827',
'order_id' => 'OBE10003',
'brief_instalment_id' => '1',
'brief_history_type_id' => '3'
)
)
)
PHP:
<?php foreach ($briefHistories as $briefHistorie) { ?>
<div class="brief-summary-row">
<div>
<?php echo TimeUtil::format( "d/M/Y", $briefHistorie['BriefHistory']['created']); ?>
</div>
</div>
<?php } ?>
答案 0 :(得分:0)
<?php
$current = TimeUtil::format( "d/M/Y", $briefHistories[0]['BriefHistory']['created']);
echo '<div class="brief-summary-row"><div>';
foreach ($briefHistories as $briefHistorie) {
$created = TimeUtil::format( "d/M/Y", $briefHistorie['BriefHistory'['created']);
echo $created;
if($created != $current){
echo '</div></div><div class="brief-summary-row"><div>';
$current = $created;
}
}
echo '</div></div>';
?>
答案 1 :(得分:0)
这些方面的东西。可以压缩:
$previous = '';
foreach ($briefHistories as $briefHistorie) {
$date = TimeUtil::format("d/M/Y", $briefHistorie['BriefHistory']['created']);
if($date != $previous) {
echo '<div class="brief-summary-row">';
}
echo '<div>'.$date.'</div>';
if($date != $previous) {
echo '</div>';
}
$previous = $date;
}
答案 2 :(得分:0)
为他人
$arrayDates = array();
$i = 0;
$key = array();
foreach ($briefHistories as $briefHistorie) {
$key_name = date('Ymd', strtotime($briefHistorie['BriefHistory']['created']));
if(!isset($key[$key_name])){
$key[$key_name]=$i;
$i++;
}
$arrayDates[$key[$key_name]][]= $briefHistorie;
}
foreach ($arrayDates as $arrayDate ) {
echo '<div class="brief-summary-row">';
foreach ($arrayDate as $data) {
echo '<strong>'.date('d-m-Y', strtotime($data['BriefHistory']['created'])).'</strong>';
}
echo '</div>';
}
print out =&gt;
<div class="brief-summary-row">
<strong>28-02-2014</strong>
</div>
<div class="brief-summary-row">
<strong>27-02-2014</strong>
<strong>27-02-2014</strong>
<strong>27-02-2014</strong>
<strong>27-02-2014</strong>
</div>
<div class="brief-summary-row">
<strong>26-02-2014</strong>
<strong>26-02-2014</strong>
</div>
<div class="brief-summary-row">
<strong>25-02-2014</strong>
</div>