PHP Sort a multidimensional array by element containing date

后端 未结 10 1848
野趣味
野趣味 2020-11-22 09:02

I have an array such as:

Array
(
[0] => Array
    (
        [id] => 2
        [type] => comment
        [text] => hey
        [datetime] => 20         


        
10条回答
  •  日久生厌
    2020-11-22 10:07

    For 'd/m/Y' dates:

    usort($array, function ($a, $b, $i = 'datetime') { 
        $t1 = strtotime(str_replace('/', '-', $a[$i]));
        $t2 = strtotime(str_replace('/', '-', $b[$i]));
    
        return $t1 > $t2;
    });
    

    where $i is the array index

提交回复
热议问题