PHP Sort a multidimensional array by element containing date

后端 未结 10 1731
野趣味
野趣味 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 09:46

    From php7 you can use the Spaceship operator:

    usort($array, function($a, $b) {
      return new DateTime($a['datetime']) <=> new DateTime($b['datetime']);
    });
    

提交回复
热议问题