2012-04-05 7 views
5

Attualmente ho un problema in PHP, in cui desidero ordinare questi post in base alla data di creazione in modo che possano essere visualizzati in ordine decrescente. Ho cercato una funzione PHP per farlo ma non ho avuto fortuna.Ordinamento di matrici all'interno di un array in PHP da datetime

C'è una soluzione facile a questo? Qualsiasi idea sarà molto apprezzato :)

array 
     0 => 
     array 
      'post_id' => string '1' (length=1) 
      'user_id' => string '3' (length=1) 
      'post' => string 'this is a post' (length=14) 
      'created' => string '2012-04-05 20:11:38' (length=19) 
    1 => 
     array 
      'post_id' => string '2' (length=1) 
      'user_id' => string '2' (length=1) 
      'post' => string 'this is a post' (length=14) 
      'created' => string '2012-04-05 20:11:38' (length=19) 
    2 => 
     array 
      'post_id' => string '3' (length=1) 
      'user_id' => string '5' (length=1) 
      'post' => string 'this is a post' (length=14) 
      'created' => string '2012-04-05 20:11:38' (length=19) 
+4

È questo proveniente da un Banca dati? Se no, perché no? – NullUserException

+0

da dove viene caricato questo array? – hjpotter92

+3

Sì, sembra sospetto come le righe di query MySQL. 'ORDER BY creato DESC' –

risposta

5

Prova questo:

<?php 
$a=array(
     0 => 
     array(
      'post_id' => '1', 
      'user_id' => '3', 
      'post' => 'this is a post', 
      'created' => '2012-04-05 20:11:40' 
     ), 
    1 => 
     array(
      'post_id' => '2', 
      'user_id' => '2', 
      'post' => 'this is a post', 
      'created' => '2012-04-05 20:11:39' 
     ), 
    2 => 
     array(
      'post_id' => '3', 
      'user_id' => '5', 
      'post' => 'this is a post', 
      'created' => '2012-04-05 20:11:38' 
     ) 
); 
function cmp($a,$b){ 
    return strtotime($a['created'])<strtotime($b['created'])?1:-1; 
}; 

uasort($a,'cmp'); 
print_r($a); 
?> 
+0

'cmp()' dovrebbe restituire un valore negativo se '$ a' è considerato minore di' $ b', un valore positivo se '$ a' è maggiore di' $ b' e 0 se sono uguali. – Arjan

+1

grazie, risposta aggiornata (ho omesso lo stesso caso, lo so ..) – stewe

+0

Non c'è bisogno di 'strtotime'. Il formato 'AAAA-MM-GG HH: MM: SS' (24 ore) ha solo bisogno della funzione' strcmp' per l'ordinamento. – m13r

1

È possibile utilizzare strtotime() per convertire il timestamp in un intero.

+2

E come può essere utile per ordinare l'array 2D? –

+0

Eh ok, domanda vaga. Pensavo che OP implicasse che i suoi francobolli fossero Y-d-m. –

+1

Se le date sono nel formato 'Y-d-m', non è possibile convertirle facilmente in data e ora. E se sono nel formato 'Y-m-d', non è necessario convertirli. – Arjan

1

È possibile ordinare un array utilizzando una funzione di ordinamento, in questo modo:

function cmp($a, $b) { 
    if($a['created'] < $b['created']) { 
     return 1; 
    } else if ($a['created'] > $b['created']) { 
     return -1; 
    } else { 
     // The only option left is that they are equal 
     return 0; 
    } 
} 

usort($array, cmp); 

Per ulteriori informazioni su usort, controllare la php manpage

+1

Questo produrrà un ordine crescente anziché decrescente. – MrCode

+1

Ho aggiornato la mia risposta. Fortunatamente è solo un piccolo cambiamento. – Arjan

1

Puoi utilizzare la funzione usort() che consente di ordinare un array in base ai propri criteri.

function cmp($a, $b) 
{ 
    if ($a['created'] == $b['created']) { 
     return 0; 
    } 

    return ($a['created'] < $b['created']) ? 1 : -1; 
} 

usort($myArray, "cmp"); 

print_r($myArray); 

Oppure, se si desidera convertire in un tempo:

function cmp($a, $b) 
{ 
    if ($a['created'] == $b['created']) { 
     return 0; 
    } 

    $aInt = strtotime($a['created']); 
    $bInt = strtotime($b['created']); 

    return ($aInt < $bInt) ? 1 : -1; 
} 
2

Ordinamento matrice di record/assoc_arrays dal campo mysql datetime specificato e in ordine:

function build_sorter($key, $dir='ASC') { 
     return function ($a, $b) use ($key, $dir) { 
      $t1=strtotime(is_array($a)?$a[$key]:$a->$key); 
      $t2=strtotime(is_array($b)?$b[$key]:$b->$key); 
      if($t1==$t2) return 0; 
      return (str_to_upper($dir)=='ASC'?($t1 < $t2):($t1 > $t2)) ? -1 : 1; 
     }; 
    } 


    // $sort - key or property name 
    // $dir - ASC/DESC sort order or empty 
    usort($arr, build_sorter($sort, $dir)); 
Problemi correlati