PHP sort multidimensional array by date

后端 未结 4 1466
遇见更好的自我
遇见更好的自我 2021-01-18 16:58

I\'m having a problem. I have a multidimensional array, that looks like this:

Array ( [0] => 
              Array ( 
                    [0] => Testguy         


        
相关标签:
4条回答
  • 2021-01-18 17:40

    I'm just stepping away from my desk for the day so I can't offer specifics. But here's a good place to get started that includes examples: array_multisort

    0 讨论(0)
  • 2021-01-18 17:44
    $dates = array();       
    foreach($a AS $val){
        $dates[] = strtotime($val[2]);
    }
    array_multisort($dates, SORT_ASC, $a);
    
    0 讨论(0)
  • 2021-01-18 17:47
    function cmp($a, $b){
    
        $a = strtotime($a[2]);
        $b = strtotime($b[2]);
    
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }
    
    usort($array, "cmp");
    

    Or for >= PHP 7

    usort($array, function($a, $b){
        return strtotime($a[2]) <=> strtotime($b[2]);
    });
    
    0 讨论(0)
  • 2021-01-18 17:53

    You can do it using usort with a Closure :

    usort($array, function($a, $b) {
        $a = strtotime($a[2]);
        $b = strtotime($b[2]);
        return (($a == $b) ? (0) : (($a > $b) ? (1) : (-1)));
    });
    
    0 讨论(0)
提交回复
热议问题