-3

I get the data from database and I want to sort hotel_name in alphabetical order. How can I do it using php's sort function or any other method ?

foreach($selData as $row)
{
    $name=$row['hotel_name']; 
    $distance_colombo=$row['distance_colombo'];
    $distance_airport=$row['distance_airport'];
    $description=$row['description'];
    $activities=$row['activities'];
    $str=(explode(',',$activities));
    print_r($str);
}
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • 3
    why don't you use `ORDER BY hotel_name` on your SQL query? – Prix Aug 31 '13 at 01:48
  • 1
    Did you seriously not find anything when you typed "php sort" into Google? – cHao Aug 31 '13 at 01:49
  • @cHao kindly replace Google with Stackoverflow ;) – Prix Aug 31 '13 at 01:50
  • prix, i alredy use order by in sql query i',s work fine... @cHao i got bunch of sort functions and i try to do that way.. what i'm asking is i don't want to sort whole array..i want only sort 'hotel_name' coulumn only using sort function... i get some errors doing using sort functions – Prabath Anuradha Aug 31 '13 at 02:52
  • @PrabathAnuradha: If all you want is the `hotel_name` column, then create an array with just that in it, and `sort($that_array)`. Otherwise, this is a duplicate of http://stackoverflow.com/q/2699086 (which was the 5th result in Google, BTW, so don't tell me you bothered), which is itself marked as a duplicate of http://stackoverflow.com/q/96759 . – cHao Aug 31 '13 at 04:32

1 Answers1

2

There's no need to use sort() here. Just use ORDER BY in your SQL query.

Example:

SELECT * FROM table_name ORDER BY hotel_name
Amal Murali
  • 75,622
  • 18
  • 128
  • 150