如何按日期对数组进行分类或分组 [英] how to categorise or group array by date
本文介绍了如何按日期对数组进行分类或分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按日期分组的历史列表DESC,如下所示:
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
我想这样打印。
-----------
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 } ?>
推荐答案
b $ b
$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 =>
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>
其他这里是数组演示
这篇关于如何按日期对数组进行分类或分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文