Sorting an array with DateTime strings?

后端 未结 6 1352
深忆病人
深忆病人 2021-01-20 03:57

How can i sort efficiently this array (recent items first) on end_time value in PHP?

array
  0 => 
    array
      \'value\' => int 4
             


        
6条回答
  •  花落未央
    2021-01-20 04:28

    Have you looked at the usortDocs function? It lets you define a callback function to compare two values. So I'm suggesting something along these lines:

    function compare_func($a, $b)
    {
        // CONVERT $a AND $b to DATE AND TIME using strtotime() function
        $t1 = strtotime($a["end_time"]);
        $t2 = strtotime($b["end_time"]);
    
        return ($t2 - $t1);
    }
    
    usort(main_array, "compare_func");
    

提交回复
热议问题