Re: Sorting a PHP array
| From: | Matt Pelmear | Date: | Wed, 15 May 2013 16:46:41 +0000 |
| Subject: | Re: Sorting a PHP array | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-48742@lists.php.net to get a copy of this message | ||
I would use ORDER BY in your query and let the database do the work, assuming you're using a standard relational database (like MySQL or Postgresql).
Otherwise, if you MUST do it in PHP, you could look at usort(), or uasort() using your own custom sorting function. I would, however, strongly recommend keeping the work in the database. (If you're using a bleeding-edge version of PHP (I think this is in the 5.5 release candidates), you could also use array_column() along with usort() or uasort() to make your life easier.)
-Matt
On 05/15/2013 09:32 AM, Charlie Lewis wrote:
I have a bookseller database list read into a PHP array with dimensions [row][field]. There are 32 fields in each record/row and up to 500 records. What is the neatest way to sort the array by any of the fields, such as author or title? I know I should just search for a manual on this, but could anybody give me a quick start? Thanks, Charlie