如何按日期分类(分组 - 分类)

时间:2014-02-28 22:11:38

标签: php arrays

如何正确打印此阵列。所有27 / Feb应该显示在Node(1)中,但其中一个正在向下一个节点进行操作。此数组已按日期分组和DESC。这是我的数组

$briefHistories = array(
    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'
        )
    ),
    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'
        )
    ),
    2 => array(
        'BriefHistory' => array(
            'id' => '4',
            'created' => '2014-02-27 16:40:50',
            'created_by' => '7827',
            'order_id' => 'OBE10003',
            'brief_instalment_id' => '2',
            'brief_history_type_id' => '4'
        )
    ),
    3 => 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'
        )
    ),
    4 => 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'
        )
    ),
    5 => 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代码

$bdate = $briefHistories[0]['BriefHistory']['created'];    

$arrayDates = array(); $i = 0;$j=0;

foreach ($briefHistories as $briefHistorie) { 
    if (date('Y-m-d', strtotime($bdate))>date('Y-m-d', strtotime($briefHistorie['BriefHistory']['created']))){ 
        $arrayDates[$i][$j] = $briefHistorie; 
    } else {
        $arrayDates[$i][$j] = $briefHistorie; 
        $i++;
    }
        $bdate = date('Y-m-d', strtotime($bdate))>date('Y-m-d', strtotime($briefHistorie['BriefHistory']['created']))?$briefHistorie['BriefHistory']['created']:$bdate; 
    $j++;
} 
debug($arrayDates);

输出>

array(
    (int) 0 => 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(
        (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' => '4',
                'created' => '2014-02-27 16:40:50',
                'created_by' => '7827',
                'order_id' => 'OBE10003',
                'brief_instalment_id' => '2',
                'brief_history_type_id' => '4'
            )
        )
    ),
    (int) 2 => array(
        (int) 3 => 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(
        (int) 4 => 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'
            )
        ),
        (int) 5 => 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'
            )
        )
    )
)

I want to show it like this list

-----------    
28/Feb/2014    
-----------
27/Feb/2014    
27/Feb/2014
27/Feb/2014  
------------    
26/Feb/2014
26/Feb/2014

1 个答案:

答案 0 :(得分:1)

试试这个

    $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;
    }