douzhi1919 2015-07-28 13:35
浏览 28
已采纳

如何在PHP中替换键和排序数组? [重复]

This question already has an answer here:

Please someone help me how to sort dates in array

i have array called $cont_history and it is a multi dimensional array, so now i need to sort this array by KEY VALUE (DATE)

   $cont_history = Array ( 
        [20-02-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [15-03-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [18-03-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [05-04-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [10-04-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        [13-05-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        [10-02-2015] => Array ( [activity] => 'Container Bought', [by] => 'CARU', [location] => 'COLOMBO', [depot] => 'CNLS' ) 
        [07-05-2015] => Array ( [activity] => 'Container Sold',   [by] => 'TCPL', [location] => 'MUMBAI',  [depot] => 'CONS' ) 
        )

after sorting the array this is what output i am expecting,

   $cont_history = Array ( 
        [20-02-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [15-03-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'COLOMBO, [depot] => 'CNLS' ) 
        [18-03-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [05-04-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'CHENNAI, [depot] => 'GOBAL') 
        [10-04-2015] => Array ( [activity] => 'GATE IN',  [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        [07-05-2015] => Array ( [activity] => 'Container Sold',   [by] => 'TCPL', [location] => 'MUMBAI',  [depot] => 'CONS' ) 
        [13-05-2015] => Array ( [activity] => 'GATE OUT', [by] => '',  [location] => 'MUMBAI,  [depot] => 'CONS' ) 
        )

the date is DD-MM-YYYY format. I dont have any idea how to do this sorting. please someone help me how to do this. thank you.

</div>
  • 写回答

1条回答 默认 最新

  • duanlachu7344 2015-07-28 13:43
    关注

    You can use "uksort()" to sort the array using a custom function:

    function sortByDate($a, $b) {
       return strtotime($a) > strtotime($b);
    }
    
    $cont_history = Array ( 
        '20-02-2015' => Array ( 'activity' => 'GATE IN',  'by' => '',  'location' => 'COLOMBO', 'depot' => 'CNLS' ), 
        '15-03-2015' => Array ( 'activity' => 'GATE OUT', 'by' => '',  'location' => 'COLOMBO', 'depot' => 'CNLS' ), 
        '18-03-2015' => Array ( 'activity' => 'GATE IN',  'by' => '',  'location' => 'CHENNAI', 'depot' => 'GOBAL'), 
        '05-04-2015' => Array ( 'activity' => 'GATE OUT', 'by' => '',  'location' => 'CHENNAI', 'depot' => 'GOBAL'), 
        '10-04-2015' => Array ( 'activity' => 'GATE IN',  'by' => '',  'location' => 'MUMBAI',  'depot' => 'CONS' ), 
        '13-05-2015' => Array ( 'activity' => 'GATE OUT', 'by' => '',  'location' => 'MUMBAI',  'depot' => 'CONS' ), 
        '10-02-2015' => Array ( 'activity' => 'Container Bought', 'by' => 'CARU', 'location' => 'COLOMBO', 'depot' => 'CNLS' ),
        '07-05-2015' => Array ( 'activity' => 'Container Sold',   'by' => 'TCPL', 'location' => 'MUMBAI',  'depot' => 'CONS' ) 
    );
    
    uksort($cont_history, 'sortByDate');
    
    print_r($cont_history);
    

    Demo: http://3v4l.org/33RZg

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于php中URL传递GET全局变量的问题
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件