SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    What are you sorting by?

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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;
    }); 
    And if you want a 'reverse' order, turn the < into a >.

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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;
    }); 
    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. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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. #7
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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


Bookmarks

Posting Permissions

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