Page 1 of 1

associative arrays sorting

Posted: Tue Nov 28, 2006 5:31 pm
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]

Posted: Tue Nov 28, 2006 5:45 pm
by feyd
You'll either need to use array_multisort() or usort().