Sort array of objects by date field

后端 未结 4 1986
天涯浪人
天涯浪人 2021-01-01 15:57

How can I re-arrange a array of objects like this:

 [495] => stdClass Object
        (
         [date] => 2009-10-31 18:24:09
         ...
        )
 [         


        
4条回答
  •  我在风中等你
    2021-01-01 16:33

    Since the original question is about sorting arrays of stdClass() objects, here's the code which would work if $a and $b are objects:

    usort($array, function($a, $b) {
        return strtotime($a->date) - strtotime($b->date);
    });
    

    Or if you don't have PHP 5.3:

    function cb($a, $b) {
        return strtotime($a->date) - strtotime($b->date);
    }
    usort($array, 'cb');
    

提交回复
热议问题