Sorting an array by a key

Ask about a PHP problem here.
Post Reply
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

Sorting an array by a key

Post by dareskog »

I have an array with several entries.

A common key inside is "date_timestamp"

Lets pretend this array is called $items

How do i organise the elements inside $items by the common key "date_timestamp" which is a unix time stamp?
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Sorting an array by a key

Post by nyo »

If you are reading the array of entries from a database, you can use "ORDER BY" (MySQL command) to sort entries based on date. Other than that, you can give ksort() function a try. Can't give specific answer without seeing your code.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Sorting an array by a key

Post by jacek »

nyo wrote:If you are reading the array of entries from a database, you can use "ORDER BY" (MySQL command) to sort entries based on date

^ The best idea :)

If you are stuck with the array as it is though you could try a usort() but it really depends on the format of it.
Image
Post Reply