mysterbx Posted October 30, 2008 Share Posted October 30, 2008 Hello, I need to somehow sort array by date... Yesterday I tried that, took me 2 hours, and didnt find anything that works... i even found 3 topics here, on phpfreaks, they also did not work... Here is my array (example, the real one has 10k records in it): Array ( [0] => Array ( [0] => 10-27-2004|Site NR1|http://www.rapidshare.com/|Rapidshare|x1|c1 [1] => 10-08-2008|Site NR2|http://www.google.com/|Google|x2|c2 ) [1] => Array ( [0] => 10-27-2004 [1] => 10-08-2008 ) [2] => Array ( [0] => Site NR1 [1] => Site NR2 ) [3] => Array ( [0] => http://www.rapidshare.com/ [1] => http://www.google.com/ ) [4] => Array ( [0] => Rapidshare [1] => Google ) [5] => Array ( [0] => x1 [1] => x2 ) [6] => Array ( [0] => c1 [1] => c2 ) ) the latest entries should be at the top. i tried some codes yesterday (usort,rsort) they change the places of arrays Array[1] is Array[6] and so on... it would be soo great if someone would make this work... THANKS! Quote Link to comment Share on other sites More sharing options...
niranjnn01 Posted October 30, 2008 Share Posted October 30, 2008 Hello , I solved a somewhat similar problem with uasort. [maintain index association unlike asort] in ur case, the data in the array is not always date.. sometimes its other information instead of date, and sometimes a combination of date and other information . So I guess you will have to write conditions for detecting that too , and then sort, into the "user defined function " that you are supplying uasort with Also make sure u understand how uasort or usort works clearly.. because that usually takes some time and can be a bit confusing . Hopefully Rakesh Quote Link to comment Share on other sites More sharing options...
mysterbx Posted October 30, 2008 Author Share Posted October 30, 2008 in array[1] contents are always date... it never changes Quote Link to comment Share on other sites More sharing options...
jithavijayan Posted October 30, 2008 Share Posted October 30, 2008 firstly you have to explode the date by day month and year after that you have to sort year first den month and den day Quote Link to comment Share on other sites More sharing options...
mysterbx Posted October 30, 2008 Author Share Posted October 30, 2008 already done: function sd($a, $b) { list($x, $y, $z) = explode("-", $a); $a = "$z-$x-$y"; list($x, $y, $z) = explode("-", $b); $b = "$z-$x-$y"; return -strcmp($a, $b); } well lets say the array is name $array i tried usort($array[1],'sd');, this only sorts the array[1], other arrays dont change i tried usort($array,'sd');, this mixes up all the array, the places change... what am i doing wrong? Quote Link to comment Share on other sites More sharing options...
mysterbx Posted October 30, 2008 Author Share Posted October 30, 2008 i spent some time in google, but still no results that make this array work as i need... : ( Quote Link to comment Share on other sites More sharing options...
mysterbx Posted October 31, 2008 Author Share Posted October 31, 2008 I found an example: $ar = array ( array ('2007-05-10', '2007-06-13', '2007-04-10'), array ('kiwi', 'pear', 'apple'), array ('car', 'bus', 'plane') ); array should be sorted by the date. LIKE: 2007-06-13>>>pear>>>bus 2007-05-10>>>kiwi>>>car 2007-04-10>>apple>>>plane please help me out here! Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 31, 2008 Share Posted October 31, 2008 where are you getting this data from? Quote Link to comment Share on other sites More sharing options...
mysterbx Posted October 31, 2008 Author Share Posted October 31, 2008 well i made an example while i was searching for an answer to my question... data (arrays) are from a file (i use file instead on mysql) arrays are made from preg_match_all which reads the file and searches for results... Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 31, 2008 Share Posted October 31, 2008 alright, i'm going to use your most recent array example. the first thing to do is to switch the dates into timestamps using strtotime(). you can then sort numerically in descending order, and use the keys from this array to grab other info: <?php $ar = array ( array ('2007-05-10', '2007-06-13', '2007-04-10'), array ('kiwi', 'pear', 'apple'), array ('car', 'bus', 'plane') ); // nab the dates in timestamp format $dates = array_map('strtotime', $ar[0]); // sort descending numerically arsort($dates, SORT_NUMERIC); // traverse the new dates array foreach ($dates AS $k => $val) { echo $ar[0][$k].'>>>'.$ar[1][$k].'>>>'.$ar[2][$k].'<br />'; } ?> the output may not be exactly what you're after, but hopefully this gives you enough information to edit it to your liking. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted October 31, 2008 Author Share Posted October 31, 2008 akitchin, OMG! that is the code i need... but i forgot to say, that my date look like (d/m/Y) is the a way to convert it in array_map action ---- im searching for the function now... Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 31, 2008 Share Posted October 31, 2008 you could create a custom function that converts it, using mktime(): function custom_mktime($date) { // explode the date list($day, $month, $year) = explode('/', $date); // pass back a timestamp return mktime(0, 0, 0, $month, $day, $year); } $dates = array_map('custom_mktime', $ar[0]); Quote Link to comment Share on other sites More sharing options...
mysterbx Posted October 31, 2008 Author Share Posted October 31, 2008 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.