0

I have a 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
            )
    )

How to sort value post_date on DESC ? my code here:

$indexarray = array();
array_multisort($indexarray, $indexarray["post_date"], SORT_DESC);
3
  • 2
    What is the result supposed to be? Commented Jul 24, 2013 at 6:59
  • I'd rather order these data directly inside their source. Commented Jul 24, 2013 at 7:04
  • If you have control over the generation of this array, I'd arrange the values like this: $indexarray[$postDate][] = array('ID' => $id, 'post_date' => $postDate). Benefit: ID and postDate form a data tuple and you can do a simple ksort. Commented Jul 24, 2013 at 7:17

4 Answers 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);

This keeps your relation between $original['ID'][$i] and $original['post_date'][$i]. Result:

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)
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Should be like:

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

Comments

0

this should work.

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

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

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.