newb Posted July 29, 2009 Share Posted July 29, 2009 im trying to sort this array desc by the date but its not working for some reason. doesnt sort at all. here is this the code: $new_array = array( file => $file, date => $date ); krsort( $new_array['date'] ); echo $new_array['date']; Link to comment https://forums.phpfreaks.com/topic/168054-solved-sort-array/ Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 what are $file and $date set to, could you post that code too? Link to comment https://forums.phpfreaks.com/topic/168054-solved-sort-array/#findComment-886375 Share on other sites More sharing options...
newb Posted July 29, 2009 Author Share Posted July 29, 2009 $file = readdir("/public_html/"); $date = date("F d Y H:i:s.",filemtime("$file")); Link to comment https://forums.phpfreaks.com/topic/168054-solved-sort-array/#findComment-886389 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 you must be looping and other things besides just the code you provided... are you opening the dir and setting the handle first, etc? if so, post some more code... Link to comment https://forums.phpfreaks.com/topic/168054-solved-sort-array/#findComment-886396 Share on other sites More sharing options...
gevans Posted July 30, 2009 Share Posted July 30, 2009 ksort is sorting by key. The key your sorting by is 'date', so everything you compare is the same. You want to sort by the value of the array key 'date' Link to comment https://forums.phpfreaks.com/topic/168054-solved-sort-array/#findComment-886397 Share on other sites More sharing options...
newb Posted July 30, 2009 Author Share Posted July 30, 2009 ksort is sorting by key. The key your sorting by is 'date', so everything you compare is the same. You want to sort by the value of the array key 'date' :s explain Link to comment https://forums.phpfreaks.com/topic/168054-solved-sort-array/#findComment-886411 Share on other sites More sharing options...
gevans Posted July 30, 2009 Share Posted July 30, 2009 http://us2.php.net/manual/en/function.ksort.php#function.ksort.examples check out that example Personally I'd do the following.... $date = filemtime("$file"); Just add the timestamp. Sort the date on output. Instead of ksort do the following; <?php array_multisort($new_array['date'], SORT_DESC, SORT_NUMERIC, $new_array['file']); Link to comment https://forums.phpfreaks.com/topic/168054-solved-sort-array/#findComment-886417 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.