#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171

    Sorting multi dimentional array by 2 elements


    Hello;
    Code:
    Array ( 
    
    [1875] => Array ( [hotel_id] => 1875 [total] => 570  [status] => b ) 
    
    [1471] => Array ( [hotel_id] => 1471 [total] => 684 [status] => r)
    
    [4] => Array ( [hotel_id] => 4 [total] => 1134 [status] => b))
    I want to sort this array to
    1 - Show all elements with [status] => b first
    2 - and then elements with smallest amount of [total].
    So the results look like this:
    Code:
    Array ( 
    
    [1875] => Array ( [hotel_id] => 1875 [total] => 570 [status] => b ) 
    
    [4] => Array ( [hotel_id] => 4 [total] => 1134 [status] => b)
    
    [1471] => Array ( [hotel_id] => 1471 [total] => 684 [status] => r)
    
    )
    I used the code below but it does not sort at all:
    PHP Code:
    foreach($lookup as $val =>$row)
        {
            
    $sorted[$val]  = $row['total']; 
            
    $status[$val] = $row['status'];
        }
    array_multisort($statusSORT_ASC$sortedSORT_ASC$lookup); 
    What did I do wrong? Thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,505
    Rep Power
    594
    That main thing you did wrong, if this is really your code, is that you did not sort anything, anywhere.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Use usort for this, there's good examples in the manual.

    Also, RE: Your signature: No work will be done on devshed in the immediate future, though rumor has it we'll see a sudden complete face lift sometime soon, leapfrogging more than a decade into the future.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    This workes fine now!
    PHP Code:
    foreach($lookup as $k=>$v)
        {
            
    $sort['status'][$k] = $v['status'];
            
    $sort['total'][$k] = $v['total'];
        }

    array_multisort($sort['status'], SORT_ASC$sort['total'], SORT_ASC,$lookup); 
    Can anyone explain why the code in the original question above didn't work?
    Last edited by zxcvbnm; April 14th, 2013 at 10:05 PM.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Your original example worked perfectly for me, though usort is most likely faster anyway.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo