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

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

    How to sort multi-dimentional array in PHP


    Hello;
    Is it possible to sort this array based on the total?
    PHP Code:
    print_r($listing); 
    Array (
    [1071] => Array ( [hotel_id] => 1071 [total] => 195 [destinationname] => Sunshine Coast [startype] => 4.5 )
    [325] => Array ( [hotel_id] => 325 [total] => 1230 [destinationname] => Sunshine Coast [startype] => 4 )
    [36] => Array ( [hotel_id] => 1109 [total] => 930 [destinationname] => Sunshine Coast [startype] => 4 )
    )
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Certainly it's possible. You can use array_multisort or usort. The documentation page for array_multisort has an example of sorting 2-dimension arrays.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    Use usort with a function that takes two of those subarrays and returns which comes first. Check the manual page and user comments for some examples how.
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Thanks this works fine.
    PHP Code:
    foreach($lookup as $val =>$row)
        {
            
    $sorted[$val]  = $row['total']; 
        }
    array_multisort($sortedSORT_ASC$lookup); 

IMN logo majestic logo threadwatch logo seochat tools logo