2017-07-07 3 views
-1

Les clés de tableau sont des dates et veulent trier les tableaux par date dans l'ordre croissant. Voici un tableau:Trier tableau par date php

Array 
(
    [07/14/2017] => Array 
     (
      [ID] => 5442 
      [post_content] => Test1 
      [post_title] => Testevents1 
     ) 

    [01/11/2017] => Array 
     (
      [ID] => 5443 
      [post_content] => Test2 
      [post_title] => Testevents2 
     ) 
) 
+0

son déjà répondu [ici] (https: // stackoverflow.com/questions/38770210/how-to-sort-array-with-date-as-key) avec plus d'explications [ici] (https://stackoverflow.com/questions/17364127/how-can-i-sort -arrays-and-data-in-php) –

+0

Voici votre réponse [réponse] (https://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array -by-element-containing-date) –

Répondre

2

Vous pouvez utiliser uksort pour le faire:

uksort($arr, function ($a, $b) { 
    $t1 = strtotime($a); 
    $t2 = strtotime($b); 
    if ($t1 == $t2) { 
     return 0; 
    } 
    return ($t1 > $t2) ? 1 : -1; 
}); 
+0

comment faire une boucle, c'est-à-dire des enregistrements multiples –

+0

@RDMage A quoi ressemble plusieurs enregistrements? Est-ce que 'for' ou' foreach' convient à votre cas? –

+0

ksort ($ new) a bien fonctionné dans foreach merci –

0

Vous pouvez utiliser uksort pour cela.

uksort - Trie un tableau par touches en utilisant une fonction de comparaison définie par l'utilisateur

function cmp($keyA, $keyB) 
{ 
    // Your date parsing and comparison 

    // The comparison function must return an integer less than, equal to, 
    // or greater than zero if the first argument is considered to be respectively 
    // less than, equal to, or greater than the second. 
    // Note that before PHP 7.0.0 this integer had to be in the range 
    // from -2147483648 to 2147483647. 
} 

uksort($arr, "cmp") 
+0

comment faire une boucle, c'est-à-dire des enregistrements multiples –