2013-07-24 3 views
0

J'ai un tableau:Comment trier tableau basé sur l'horodatage en php

Array 
    (
     [ID] => Array 
      (
       [0] => 45335 
       [1] => 44403 
       [2] => 45734 
       [3] => 44494 
       [4] => 46869 
       [5] => 46895 
       [6] => 47481 
       [7] => 48788 
       [8] => 43950 
       [9] => 43960 
       [10] => 43979 
      ) 
     [post_date] => Array 
      (
       [0] => 1373812230 
       [1] => 1373835652 
       [2] => 1373900427 
       [3] => 1373922044 
       [4] => 1374087613 
       [5] => 1374094854 
       [6] => 1374354008 
       [7] => 1374613236 
       [8] => 1373547614 
       [9] => 1373558434 
       [10] => 1373569213 
      ) 
    ) 

Comment trier valeur POST_DATE sur DESC? mon code ici:

$indexarray = array(); 
array_multisort($indexarray, $indexarray["post_date"], SORT_DESC); 
+2

Quel est le résultat supposé être? – deceze

+0

Je préfère commander ces données directement dans leur source. –

+0

Si vous avez le contrôle sur la génération de ce tableau, j'arrangerais les valeurs comme ceci: '$ indexarray [$ postDate] [] = tableau ('ID' => $ id, 'post_date' => $ postDate)' . Avantage: ID et postDate forment un tuple de données et vous pouvez faire un simple 'ksort'. – stef77

Répondre

0

devrait être comme:

array_multisort($indexarray["post_date"], SORT_DESC, SORT_STRING); 
1
<?php 
$original = array (
    'ID' => array (
     0 => 45335, 
     1 => 44403, 
     2 => 45734, 
     3 => 44494, 
     4 => 46869, 
     5 => 46895, 
     6 => 47481, 
     7 => 48788, 
     8 => 43950, 
     9 => 43960, 
     10 => 43979, 
    ), 
    'post_date' => array 
    (
     0 => 1373812230, 
     1 => 1373835652, 
     2 => 1373900427, 
     3 => 1373922044, 
     4 => 1374087613, 
     5 => 1374094854, 
     6 => 1374354008, 
     7 => 1374613236, 
     8 => 1373547614, 
     9 => 1373558434, 
     10 => 1373569213, 
    ) 
); 

array_multisort($original['post_date'], SORT_DESC, $original['ID']); 

var_dump($original); 

Cela permet de maintenir votre relation entre $original['ID'][$i] et $original['post_date'][$i]. Résultat:

array(2) { 
    'ID' => 
    array(11) { 
    [0] => 
    int(48788) 
    [1] => 
    int(47481) 
    [2] => 
    int(46895) 
    [3] => 
    int(46869) 
    [4] => 
    int(44494) 
    [5] => 
    int(45734) 
    [6] => 
    int(44403) 
    [7] => 
    int(45335) 
    [8] => 
    int(43979) 
    [9] => 
    int(43960) 
    [10] => 
    int(43950) 
    } 
    'post_date' => 
    array(11) { 
    [0] => 
    int(1374613236) 
    [1] => 
    int(1374354008) 
    [2] => 
    int(1374094854) 
    [3] => 
    int(1374087613) 
    [4] => 
    int(1373922044) 
    [5] => 
    int(1373900427) 
    [6] => 
    int(1373835652) 
    [7] => 
    int(1373812230) 
    [8] => 
    int(1373569213) 
    [9] => 
    int(1373558434) 
    [10] => 
    int(1373547614) 
    } 
} 
0

Essayez ceci:

<?php 
$array = array (
'ID' => array (
    0 => 45335, 
    1 => 44403, 
    2 => 45734, 
    3 => 44494, 
    4 => 46869, 
    5 => 46895, 
    6 => 47481, 
    7 => 48788, 
    8 => 43950, 
    9 => 43960, 
    10 => 43979, 
), 
'post_date' => array 
(
    0 => 1373812230, 
    1 => 1373835652, 
    2 => 1373900427, 
    3 => 1373922044, 
    4 => 1374087613, 
    5 => 1374094854, 
    6 => 1374354008, 
    7 => 1374613236, 
    8 => 1373547614, 
    9 => 1373558434, 
    10 => 1373569213, 
) 
); 

array_multisort($array['post_date'], SORT_DESC, $array['post_date']); 
print_r($array); 
Questions connexes