排序对象数组

时间:2015-02-06 07:08:51

标签: php arrays

我有一个像:

这样的数组
array(3) { 
     [0] => object(stdClass)#322 (2) { 
       ["date"]=> string(10) "07-01-2015" 
       ["details"]=> string(191) "Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event Major Event" 
     } 
     [1]=> object(stdClass)#323 (2) { 
       ["date"]=> string(10) "06-01-2015" 
       ["details"]=> string(11) "Major Event" 
     } 
     [2]=> object(stdClass)#324 (2) { 
       ["date"]=> string(10) "14-01-2015" 
       ["details"]=> string(11) "Major Event" 
     } 
} 

我想对此进行排序。那怎么可能呢?

1 个答案:

答案 0 :(得分:0)

这看起来像是php中var_dump数组的输​​出所以我假设这是在php中你应该使用usort和自定义比较函数

function date_compare($a, $b)
{
    $t1 = strtotime($a->date);
    $t2 = strtotime($b->date);
    return $t1 - $t2;
}    
usort($array, 'date_compare');