sort array based on the dateTime in php

后端 未结 6 1445
予麋鹿
予麋鹿 2020-12-05 13:37
Array
        (
            [0] => Array
                (
                    [dateTime] => 2011-10-18 0:0:00
                    [chanl1] => 20.7
                 


        
6条回答
  •  隐瞒了意图╮
    2020-12-05 14:23

    Using uasort() with a custom sort callback should do it:

    function cmp($a, $b) {
      if ($a['dateTime'] == $b['dateTime']) {
        return 0;
      }
    
      return ($a['dateTime'] < $b['dateTime']) ? -1 : 1;
    }
    
    uasort($arr, 'cmp');
    

    uasort() preserves your array's keys, you can use usort() instead if you don't need that to happen.

提交回复
热议问题