如何类(组 - 分类)阵列按日期 [英] how to categories(group - classify) array by date

查看:130
本文介绍了如何类(组 - 分类)阵列按日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能正确地打印这个数组。所有的27 /二月应节点被示出(1),但是,那么一个goning到下一个节点。该阵列已分组,按日期DESC。这里是我的数组

  $ briefHistories =阵列(
    0 =>阵列(
        BriefHistory'=>阵列(
            'ID'=> '2',
            创造= GT; 2014年2月28日14时51分08秒,
            CREATED_BY'=> '7827',
            ORDER_ID'=> OBE10003',
            brief_instalment_id'=> '2',
            brief_history_type_id'=> '1'
        )
    )
    1 =>阵列(
        BriefHistory'=>阵列(
            'ID'=> '4',
            创造= GT; 2014年2月27日16点18分40秒,
            CREATED_BY'=> '7827',
            ORDER_ID'=> OBE10003',
            brief_instalment_id'=> '2',
            brief_history_type_id'=> '4'
        )
    )
    2 =>阵列(
        BriefHistory'=>阵列(
            'ID'=> '4',
            创造= GT; 2014年2月27日16点40分50秒,
            CREATED_BY'=> '7827',
            ORDER_ID'=> OBE10003',
            brief_instalment_id'=> '2',
            brief_history_type_id'=> '4'
        )
    )
    3 =>阵列(
        BriefHistory'=>阵列(
            'ID'=> '1',
            创造= GT; 2014年2月27日14时51分08秒,
            CREATED_BY'=> '7827',
            ORDER_ID'=> OBE10003',
            brief_instalment_id'=> '1',
            brief_history_type_id'=> '1'
        )
    )
    4 =>阵列(
        BriefHistory'=>阵列(
            'ID'=> '3',
            创造= GT; 2014年2月26日十六时18分09秒,
            CREATED_BY'=> '7827',
            ORDER_ID'=> OBE10003',
            brief_instalment_id'=> '1',
            brief_history_type_id'=> '3'
        )
    )
    5 =>阵列(
        BriefHistory'=>阵列(
            'ID'=> '3',
            创造= GT; 2014年2月26日十六时18分09秒,
            CREATED_BY'=> '7827',
            ORDER_ID'=> OBE10003',
            brief_instalment_id'=> '1',
            brief_history_type_id'=> '3'
        )
    )
);

// PHP的code

  $ bdate = $ briefHistories [0] ['BriefHistory'] ['创建'];$ arrayDates =阵列(); $ I = 0; $ J = 0;的foreach($ briefHistories为$ briefHistorie){
    如果(日期('Y-M-D时的strtotime($ bdate))>日期('Y-M-D时的strtotime($ briefHistorie ['BriefHistory'] ['创建']))){
        $ arrayDates [$ i] [$ J] = $ briefHistorie;
    }其他{
        $ arrayDates [$ i] [$ J] = $ briefHistorie;
        $ I ++;
    }
        $ bdate =日期('Y-M-D时的strtotime($ bdate))>日期('Y-M-D', strtotime($briefHistorie['BriefHistory']['created']))?$briefHistorie['BriefHistory']['created']:$bdate;
    $ J ++;
}
调试($ arrayDates);

输出>

 阵列(
    (INT)0 =>阵列(
        (INT)0 =>阵列(
            BriefHistory'=>阵列(
                'ID'=> '2',
                创造= GT; 2014年2月28日14时51分08秒,
                CREATED_BY'=> '7827',
                ORDER_ID'=> OBE10003',
                brief_instalment_id'=> '2',
                brief_history_type_id'=> '1'
            )
        )
    )
    (INT)1 =>阵列(
        (INT)1 =>阵列(
            BriefHistory'=>阵列(
                'ID'=> '4',
                创造= GT; 2014年2月27日16点18分40秒,
                CREATED_BY'=> '7827',
                ORDER_ID'=> OBE10003',
                brief_instalment_id'=> '2',
                brief_history_type_id'=> '4'
            )
        )
        (INT)2 =>阵列(
            BriefHistory'=>阵列(
                'ID'=> '4',
                创造= GT; 2014年2月27日16点40分50秒,
                CREATED_BY'=> '7827',
                ORDER_ID'=> OBE10003',
                brief_instalment_id'=> '2',
                brief_history_type_id'=> '4'
            )
        )
    )
    (INT)2 =>阵列(
        (INT)3 =>阵列(
            BriefHistory'=>阵列(
                'ID'=> '1',
                创造= GT; 2014年2月27日14时51分08秒,
                CREATED_BY'=> '7827',
                ORDER_ID'=> OBE10003',
                brief_instalment_id'=> '1',
                brief_history_type_id'=> '1'
            )
        )
    )
    (INT)3 =>阵列(
        (INT)4 =>阵列(
            BriefHistory'=>阵列(
                'ID'=> '3',
                创造= GT; 2014年2月26日十六时18分09秒,
                CREATED_BY'=> '7827',
                ORDER_ID'=> OBE10003',
                brief_instalment_id'=> '1',
                brief_history_type_id'=> '3'
            )
        )
        (INT)5 =>阵列(
            BriefHistory'=>阵列(
                'ID'=> '3',
                创造= GT; 2014年2月26日十六时18分09秒,
                CREATED_BY'=> '7827',
                ORDER_ID'=> OBE10003',
                brief_instalment_id'=> '1',
                brief_history_type_id'=> '3'
            )
        )
    )

我想表明它像这样列表

  -----------
28 /二月/ 2014
-----------
27 /二月/ 2014
27 /二月/ 2014
27 /二月/ 2014
------------
26 /二月/ 2014
26 /二月/ 2014


解决方案

试试这个

  $ arrayDates =阵列();
    $ I = 0;
    $键=阵列();
    的foreach($ briefHistories为$ briefHistorie){
        $ KEY_NAME =日期(YMD的strtotime($ briefHistorie ['BriefHistory'] ['创建']));
        如果(!使用isset($键[$ KEY_NAME])){
            $键[$ KEY_NAME] = $ I;
            $ I ++;
        }
        $ arrayDates [$键[$ KEY_NAME] [] = $ briefHistorie;
    }

How can I print this array correctly. All the 27/Feb should be shown in Node(1) but one of then is goning to next node. This array has been grouped and DESC by date. Here is my array

$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 code

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

output>

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

解决方案

Try this

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

这篇关于如何类(组 - 分类)阵列按日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆