2013-04-07 23 views
5

Sorun yaşıyorum.PHP, çok boyutlu diziyi tarihe göre sırala

Array ([0] => 
       Array ( 
        [0] => Testguy2's post. 
        [1] => testguy2 
        [2] => 2013-04-03 
      ) 

     [1] => Array ( 
        [0] => Testguy's post. 
        [1] => testguy 
        [2] => 2013-04-07 
      ) 
); 

Ben eski tarihe en yeni tarihten itibaren mesajları sıralamak istiyorsanız, bu nedenle şuna benzer:: Ben çok boyutlu bir dizi var, o şöyle

Array ([1] => Array ( 
        [0] => Testguy's post. 
        [1] => testguy 
        [2] => 2013-04-07 
       ) 
     [0] => Array ( 
        [0] => Testguy2's post. 
        [1] => testguy2 
        [2] => 2013-04-03 
       ) 
); 

Nasıl sıralama yapmak o?

cevap

4
function cmp($a, $b){ 

    $a = strtotime($a[2]); 
    $b = strtotime($b[2]); 

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

usort($array, "cmp"); 
+0

Bu iyi çalışır ... –

4

Sen yapabildiği tek şey bir Closure ile usort kullanarak:

usort($array, function($a, $b) { 
    $a = strtotime($a[2]); 
    $b = strtotime($b[2]); 
    return (($a == $b) ? (0) : (($a > $b) ? (1) : (-1))); 
}); 
1
$dates = array();  
foreach($a AS $val){ 
    $dates[] = strtotime($val[2]); 
} 
array_multisort($dates, SORT_ASC, $a); 
+0

O güzel olmak düşünülebilir Ama burada örnekler içerdiğini başlamak için iyi bir yerdir kodunuzun etrafına bazı açıklamalar eklemek için. – zx485