PHP Sort a multidimensional array by element containing date

后端 未结 10 1854
野趣味
野趣味 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:01

    You can simply solve this problem using usort() with callback function. No need to write any custom function.

    $your_date_field_name = 'datetime';
    usort($your_given_array_name, function ($a, $b) use (&$your_date_field_name) {
        return strtotime($a[$your_date_field_name]) - strtotime($b[$your_date_field_name]);
    });
    

提交回复
热议问题