Sorting a Multi-Dimensional Array: Reversing the Order

Hi,

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

For instance,

$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,

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,

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

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!

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 :smiley:

yeah, that would have been helpful :stuck_out_tongue_winking_eye:

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 >.

it will be sorted by date in the final output,

i should have had this in the original!

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!

What are you sorting by?