-1

I have an array of arrays. I want to order these arrays by their ['date'] field. So the latest is at index [0]. How can I do this?

Here is an example of my print_r

$array=    (
    [0] => Array
        (
            [date] => 01/05/2013 12:00
            [location] => Town Hall
            [minutes] => mydomain.com
            [agenda] => 
        ),
 [1] => Array
        (
            [date] => 09/05/2013 12:00
            [location] => Town Hall
            [minutes] => mydomain.com/walker
            [agenda] => 
        )

)
4

2 回答 2

1

试试这个 -

$array = array(
    0 => array('date'=>'01/05/2013 12:00', 'location'=>'A'),
    1 => array('date'=>'09/05/2013 12:00', 'location'=>'B'),
    2 => array('date'=>'03/05/2013 12:00', 'location'=>'C'),
    3 => array('date'=>'02/05/2013 12:00', 'location'=>'D')
);

echo '<pre>';
print_r($array);
echo '</pre>';

function sorting($a, $b){
    $a = strtotime($a['date']);
    $b = strtotime($b['date']);

    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

usort($array, "sorting");

echo '<pre>';
print_r($array);
echo '</pre>';

观看现场演示 - http://codepad.org/biXJgHQA

于 2013-05-28T08:43:09.563 回答
0

尝试这个:

usort($array,function($a,$b){
    return strcmp($a['date'],$b['date']);
}

或者如果 PHP < 5.3

function my_sort($a,$b){
    return strcmp($a['date'],$b['date']);
}
usort($array,'my_sort');
于 2013-05-28T08:30:38.473 回答