如何根据php中的时间戳排序数组

时间:2013-07-24 06:52:57

标签: 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
            )
    )

如何在DESC上对post_date值进行排序? 我的代码在这里:

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

4 个答案:

答案 0 :(得分: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);

这可以保持$original['ID'][$i]$original['post_date'][$i]之间的关系。结果:

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)
  }
}

答案 1 :(得分:0)

应该像:

array_multisort($indexarray["post_date"], SORT_DESC, SORT_STRING);

答案 2 :(得分:0)

这应该有效。

array_multisort($ arr [“post_date”],SORT_DESC,$ arr [“ID”],SORT_DESC);

参考http://php.net/manual/en/function.array-multisort.php

答案 3 :(得分:0)

试试这个:

<?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);