teamv Posted January 13, 2008 Share Posted January 13, 2008 Hi all, I have a large array filled with other arrays, each of which is slightly different, but they all have an index "time" which is a unix timestamp. Is there any easy function which will sort this arrays of array for me by that time index (highest to lowest), or will I have to use some kind of more complex recursive algorithm? Would greatly appreciate any advice people can offer me on this question. Thanks in advance for any and all suggestions. Richard Quote Link to comment Share on other sites More sharing options...
nikefido Posted January 13, 2008 Share Posted January 13, 2008 usort, uksrt, uasort. (check out php.net for usage/examples) www.php.net/usort - basic sorting function www.php.net/uasort - maintains index association www.php.net/uksort - sort based on keys There will take your array and a user-defined function and sort your multi-dim. arrays typical user defined functions might be something like: function mySort1 ($x, $y) { return ($x['key1'] > $y['Key1']); } and you implement it like: usort($array, 'mySort1');//usort provides the function arguments Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 13, 2008 Share Posted January 13, 2008 Try: <?php function mysort($a,$b){ return $a['time'] - $b['time']; } uasort($yourarray,'mysort'); ?> Quote Link to comment Share on other sites More sharing options...
teamv Posted January 13, 2008 Author Share Posted January 13, 2008 Thank you so much for the quick responses! I will have a go at implementing the above examples and let you both know how it goes. Quote Link to comment Share on other sites More sharing options...
teamv Posted January 13, 2008 Author Share Posted January 13, 2008 Thank guys, it worked perfectly! I used: // function to sort the array by time function mysort($a,$b){ return $a['time'] < $b['time']; } // sort the array uasort($array,'mysort'); Thanks again! Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2008 Share Posted January 13, 2008 I'd be wary of relying on that solution. Your function only returns true or false. whereas usort() etc require the custom function should return a negative , zero or a positive result (as GingerRobot's does) depending on whether A should sort above, is equal to, or should sort below B Quote Link to comment Share on other sites More sharing options...
teamv Posted January 13, 2008 Author Share Posted January 13, 2008 Thank you for the advice, so to clarify I should change uasort to usort instead? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2008 Share Posted January 13, 2008 No. I'm saying your mysort() function is wrong and should be like GingerRobots Quote Link to comment Share on other sites More sharing options...
teamv Posted January 13, 2008 Author Share Posted January 13, 2008 No. I'm saying your mysort() function is wrong and should be like GingerRobots Oh, well I did try theirs first but it was sorted in the opposite order than I wanted. I then tried changing the - to a + sign as I expected that to switch it round, but it didn't. What I am missing? Thanks for all your help support thus far! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 13, 2008 Share Posted January 13, 2008 You need to switch the variables around, rather than the sign. <?php function mysort($a,$b){ return $b['time'] - $a['time']; } uasort($yourarray,'mysort'); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2008 Share Posted January 13, 2008 to switch the sign to give a DESC sort return -($a['time'] - $b['time']); which resolves to return ($b['time'] - $a['time']); (as GR states above) Quote Link to comment Share on other sites More sharing options...
teamv Posted January 13, 2008 Author Share Posted January 13, 2008 Ah that makes a lot more sense than a plus sign , I should have tried to think through the logic of what was happening in the function rather than just making a change like I did, sorry. I have now made the changes given above though and all is working well! Well thanks for sticking with me guys, and for all your help. You've been great! Richard 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.