Sorting Arrays question

I have the following:

$data[$i][content] - html
$data[$i][duedate] - unix timestamp

I simply want to sort the $data by the duedate timestamp ascending. I cannot for the life of me figure out how to do this so that when I print $data, the content is organized by the duedate.

Thanks for any help!
Josh

Josh,

You are only showing a small part of your code. Previously to the code you displayed, did you do something like:

SELECT * FROM $tbl_name ORDER BY $srt ASC

Just use your table name and the field you want to use for sorting.

I haven’t tried it using date/time, but it should work.

like josh said above it really depends where you are getting your data from and what you plan to do with it but a possibility could be to use your timestamp as an array index

eg
$data[duedate][content]
$data[duedate][more content]
$data[duedate][even more content]

and then use one of the array sort functions to order the array by the timestamp…

again you would need to show a bit more of your code to be clear of what you actually trying to achieve

hope it helps

Genius ozone! Works perfectly.

Basically I have data coming forms several databases stored in variables. I’m definitely not up on my SQL game to figure it all out, so I have data stored in arrays. It’s just for me though, so it works fine.

Thanks! That worked wonderfully!

You could use the usort function to sort the array using a user-defined comparison function.

For php < 5.3, you could use this one.

function _cmp($a, $b) { return $a['duedate'] > $b['duedate']; }
usort($data, "_cmp");

For php >= 5.3, you could you a lambda function as callback.

usort($data, function($a, $b) { return $a['duedate'] > $b['duedate']; });