问题
I have a list of history which is grouped by date DESC like this:
-----------
28/Feb/2014
-----------
27/Feb/2014
-----------
27/Feb/2014
-----------
27/Feb/2014
------------
26/Feb/2014
------------
26/Feb/2014
I want to be printed like this.
-----------
28/Feb/2014
-----------
27/Feb/2014
27/Feb/2014
27/Feb/2014
------------
26/Feb/2014
26/Feb/2014
here is my array:
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 } ?>
回答1:
<?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>';
?>
回答2:
Something along these lines. Could be compacted:
$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;
}
回答3:
for others
$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 =>
<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>
for other Here is array demo
来源:https://stackoverflow.com/questions/22102004/how-to-categorise-or-group-array-by-date