associative arrays sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iskm218st10
Forum Newbie
Posts: 1
Joined: Tue Nov 28, 2006 5:18 pm

associative arrays sorting

Post by iskm218st10 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I need to sort an associative array by any of the fields in the array, such as author or title or publisher or category.  See array below
All I know is

Code: Select all

asort($books);
I thought I could do this, but it doesn't work

Code: Select all

asort($books[author]);
Does anyone know how do this?

Code: Select all

array_push($books,array("category" => "Computer","title" => "PHP Code","author" => "Guelier","publisher" => "Orn","price" => "39.99"));
array_push($books,array("category" => "Business","title" => "Business Leaders","author" => "Oneil","publisher" => "IBD","price" => "9.99"));
array_push($books,array("category" => "Business","title" => "How to Make Money","author" => "Richy","publisher" => "Hall","price" => "49.99"));
array_push($books,array("category" => "Romance","title" => "Something Wicked","author" => "Roberts","publisher" => "Lak","price" => "9.99"));
array_push($books,array("category" => "Romance","title" => "Twice Kissed","author" => "Jackson","publisher" => "McGraw","price" => "24.99"));

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You'll either need to use array_multisort() or usort().
Post Reply