Page 1 of 1

Sorting an array by a key

Posted: Tue Nov 27, 2012 7:14 pm
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?

Re: Sorting an array by a key

Posted: Wed Nov 28, 2012 8:35 am
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.

Re: Sorting an array by a key

Posted: Thu Nov 29, 2012 1:29 am
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.