0

我有一个数组:

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

4 回答 4

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)
  }
}
于 2013-07-24T07:09:50.327 回答
0

应该是这样的:

array_multisort($indexarray["post_date"], SORT_DESC, SORT_STRING);
于 2013-07-24T07:00:51.833 回答
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);
于 2013-07-24T07:36:23.947 回答
0

这应该有效。

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

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

于 2013-07-24T07:14:18.070 回答