sort array based on the dateTime in php

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


        
相关标签:
6条回答
  • 2020-12-05 14:21

    Use usort() function with custom comparator:

    $arr = array(...);
    
    usort($arr, function($a, $b) {
      $ad = new DateTime($a['dateTime']);
      $bd = new DateTime($b['dateTime']);
    
      if ($ad == $bd) {
        return 0;
      }
    
      return $ad < $bd ? -1 : 1;
    });
    

    DateTime class has overloaded comparison operators (<, >, ==).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-05 14:29

    Simple sorting for DateTime objects with the spaceship operator:

    $list = [
        new DateTime('yesterday'),
        new DateTime('today'),
        new DateTime('1 week ago'),
    ];
    
    uasort($list, function ($a, $b) {
        return $a <=> $b;
    });
    
    var_dump($list);
    
    0 讨论(0)
  • 2020-12-05 14:31

    Use array_multisort :

     <?php
            $myarray=array(
                        0 => array
                            (
                                'dateTime' => '2011-10-18 00:0:00',
                                'chanl1' => '20.7',
                                'chanl2' => '45.4',
                                'chanl3' => '',
                            ),
    
                        1 => array
                            (
                                'dateTime' => '2011-10-18 00:15:00',
                                'chanl1' => '20.7',
                                'chanl2' => '45.4',
                                'chanl3' => '',
                            ),
                      2 => array
                            (
                                'dateTime' => '2011-10-18 00:14:00',
                                'chanl1' => '20.7',
                                'chanl2' => '33.8',
                                'chanl3' => '',
                            ),
    
                        3 => array
                            (
                                'dateTime' => '2011-10-18 00:29:00',
                                'chanl1' => '20.6',
                                'chanl2' => '33.9',
                                'chanl3' => ''
                            ));
    
                foreach($myarray as $c=>$key) {
                        $dateTime[] = $key['dateTime'];
                    $chanl1[] = $key['chanl1'];
                        $chanl2[] = $key['chanl2'];
                        $chanl3[] = $key['chanl3'];
                }
    
            array_multisort($dateTime,SORT_ASC,SORT_STRING,$myarray);   
            print_r($myarray);
    
    0 讨论(0)
  • 2020-12-05 14:39

    For performance, this method using array_multisort is very efficient:

    $ord = array();
    foreach ($array as $key => $value){
        $ord[] = strtotime($value['dateTime']);
    }
    array_multisort($ord, SORT_ASC, $array);
    print_r($array);
    
    0 讨论(0)
  • For php array and object this example is helpful for sorting...

    For datetime descending:

    usort($response_data['callback'], function($a, $b) {
       return strcasecmp(strtotime($b->view_date), strtotime($a->view_date));
    });
    

    For datetime ascending:

    usort($response_data['callback'], function($a, $b) {
       return strtotime($a->view_date) - strtotime($b->view_date);
    });
    
    0 讨论(0)
提交回复
热议问题