# Thread: Sorting a Multi-Dimensional Array: Reversing the Order

1. ## Sorting a Multi-Dimensional Array: Reversing the Order

Hi,

How can I sort a multi-dimensional array in a reverse order?

For instance,

PHP Code:
``` \$songs =  array(        '3' => array('artist'=>'The Smashing Pumpkins', 'songname'=>'Soma'),        '4' => array('artist'=>'The Decemberists', 'songname'=>'The Island'),        '1' => array('artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News'),        '2' => array('artist'=>'Jack Johnson', 'songname' =>'Only the Ocean')    );    print_r(\$songs);  ```
will result in this,

PHP Code:
``` Array(    [3] => Array        (            [artist] => The Smashing Pumpkins            [songname] => Soma        )    [4] => Array        (            [artist] => The Decemberists            [songname] => The Island        )    [1] => Array        (            [artist] => Fleetwood Mac            [songname] => Second-hand News        )    [2] => Array        (            [artist] => Jack Johnson            [songname] => Only the Ocean        ))  ```
but I would like to have it in this order instead,

PHP Code:
``` Array(   [2] => Array        (            [artist] => Jack Johnson            [songname] => Only the Ocean        ) [1] => Array        (            [artist] => Fleetwood Mac            [songname] => Second-hand News        )  [4] => Array        (            [artist] => The Decemberists            [songname] => The Island        )    [3] => Array        (            [artist] => The Smashing Pumpkins            [songname] => Soma        ))  ```
I want to keep the key values which are [2],[1],[4],[3] (the original order is [3],[4],[1],[2]).

do I need to write a function to do this?

Many thanks,
Lau

2. What are you sorting by?

3. Originally Posted by StarLion
What are you sorting by?
it will be sorted by date in the final output,

i should have had this in the original!

PHP Code:
``` Array (     [3] => Array         (             [artist] => The Smashing Pumpkins             [songname] => Soma             [date] => 1276646720         )     [4] => Array         (             [artist] => The Decemberists             [songname] => The Island             [date] => 1276646724         )     [1] => Array         (             [artist] => Fleetwood Mac             [songname] => Second-hand News             [date] => 1276646728         )     [2] => Array         (             [artist] => Jack Johnson             [songname] => Only the Ocean             [date] => 1276646731         ) )  ```
thanks!

4. yeah, that would have been helpful ;P
PHP Code:
``` uasort(\$array,function (\$a, \$b){    if (\$a['date'] == \$b['date']) {        return 0;    }    return (\$a['date'] < \$b['date']) ? -1 : 1;});  ```
And if you want a 'reverse' order, turn the < into a >.

5. Originally Posted by StarLion
yeah, that would have been helpful ;P
PHP Code:
``` uasort(\$array,function (\$a, \$b) {     if (\$a['date'] == \$b['date']) {         return 0;     }     return (\$a['date'] < \$b['date']) ? -1 : 1; });  ```
And if you want a 'reverse' order, turn the < into a >.
thanks for this!

can I ask what should I pass into \$a and \$b?

My array is \$songs so I thought I need to to change it to \$a but it says,

Warning: uasort() expects parameter 1 to be array, null given in C:\wamp\www\...

sorry!

6. dont replace \$a, replace \$array at the beginning.
\$a and \$b are internal function variables. (The second parameter of uasort is a function definition or name)

7. Originally Posted by StarLion
dont replace \$a, replace \$array at the beginning.
\$a and \$b are internal function variables. (The second parameter of uasort is a function definition or name)
oh i see. got it! thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•