mkosmosports Posted November 10, 2007 Share Posted November 10, 2007 Hey, Ive got the following md array Array ( [0] => Array ( [id] => 3 [title] => Title [date] => Oct 29 08:11 ) [1] => Array ( [id] => 5 [title] => Title [date] => Oct 29 08:25 ) [2] => Array ( [id] => 6 [title] => Title [date] => Oct 29 08:18 ) [3] => Array ( [id] => 4 [title] => Title [date] => Oct 29 08:16 ) What I want to do is sort the 1st level array by the date in the second level array (with the keys getting reordered accordingly) so this one would read as follows after: Array ( [0] => Array ( [id] => 6 [title] => Title [date] => Oct 29 08:18 ) [1] => Array ( [id] => 5 [title] => Title [date] => Oct 29 08:17 ) [2] => Array ( [id] => 4 [title] => Title [date] => Oct 29 08:16 ) [3] => Array ( [id] => 3 [title] => Title [date] => Oct 29 08:11 ) Any ideas? Thanks! Quote Link to comment Share on other sites More sharing options...
Barand Posted November 10, 2007 Share Posted November 10, 2007 try <?php $data = array ( 0 => array ( 'id' => 3, 'title' => 'Title', 'date' => 'Oct 29 08:11' ), 1 => array ( 'id' => 5, 'title' => 'Title', 'date' => 'Oct 29 08:25' ), 2 => array ( 'id' => 6, 'title' => 'Title', 'date' => 'Oct 29 08:18' ), 3 => array ( 'id' => 4, 'title' => 'Title', 'date' => 'Oct 29 08:16' ) ); function datesort ($a, $b) { $da = strtotime ($a['date']); $db = strtotime ($b['date']); return ($db - $da); } usort ($data, 'datesort'); echo '<pre>', print_r($data, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
mkosmosports Posted November 10, 2007 Author Share Posted November 10, 2007 Thanks a million Barand. Thats just what I need, works great. I was also on the same usort path, but didn't think to use the strtotime like you did.. ??? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 10, 2007 Share Posted November 10, 2007 With the dates in your sample a strcmp() would sufffice but if you had a Nov or Sep in there then they would upset the applecart without the strtotime(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.