aaelghat Posted December 10, 2010 Share Posted December 10, 2010 Hi - I'm modifying some PHP code that someone else wrote. Below is a snippet of code that basically takes mp3 files files within all the folders in a directory, and creates an RSS feed. In the resulting feed, the files are intermingled in date order. Instead, I'd like them sorted by the directory that they are in. So, if there are files in these directories: 01-First 02-Second 03-Third I'd like all the files in the "01-First" directory to come before the "02-Second" directory and so on. Within each directory the files can be sorted as they currently are. The name of the directory in the code is signified by $dir_name. I tried sort() and array_multisort() but I couldn't get either of them to work the way I wanted. Any thoughts? if ($url_params['mode'] == 'rss2') { print getRSSHeader('UTF-8', $device, $vm_config, $vm_name, $web_master, $logo_image,$context); $mailboxes = array(); if ($dev_dir = @opendir($VMHome . $device)) { //might fail if mailbox has not received first message yet while ($f = readdir($dev_dir)) { if (!eregi("tmp","$f") && !eregi("\.+","$f") && !eregi("Exclude-From-CD", "$f")) { //skip tmp directory and Exclude-From-CD Directories array_push($mailboxes,"$f"); #collect all existing mailboxes } } array_multisort($mailboxes, SORT_DESC); foreach ($mailboxes as $mailbox) { $files=array(); $dir_name = $VMHome . $device . '/'. $mailbox .'/'; $dir = opendir($dir_name); while ($f = readdir($dir)) { if (eregi("\.txt",$f)){ #if filename matches .txt in the name array_push($files,"$f"); #push into $files array } } foreach($files as $file){ $props = getProperties($dir_name, $file); $uid = getMP3File($dir_name, $file, $props, $device, $mailbox, $vm_config, $vm_name,$context); print getRSSItem ($props, $uid, $dir_name, $device, '/' . $mailbox . '/', $context); } } } print ' </channel> </rss>'; } Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 $array[$dirname][]=$filename; $keyarray[]=$dirname; sort($keyarray); for($i=0;$i<count($keyarray);$i++){ sort($array[$i]); for($i2=0;$i2<count($array[$i]);$i2++){ $finalarray[]=$array[$i][$i2]; } } would this be something along the lines of what you want? it sorts by dirname and then filename, and in the end puts it all in a final array. Quote Link to comment Share on other sites More sharing options...
aaelghat Posted December 10, 2010 Author Share Posted December 10, 2010 Thanks for the reply! I don't even need it to sort by dirname (within a directory I just need the files from oldest to newest which is what it's doing now). I'm really a PHP newbie and this isn't my code. The array I want to sort is $mailboxes, so given that I need to sort $mailboxes by the one primary sort by dirname, what would it look like, and where in the code would I put it? Thanks!! Quote Link to comment Share on other sites More sharing options...
aaelghat Posted December 10, 2010 Author Share Posted December 10, 2010 Sorry, I meant to say that I don't need it to sort by filename. I just need it to sort by dirname, and then by date, but that seems to be the how the array is naturally being sorted anyway (probably because a directory listing lists the files from oldest to newest). But I do need some help structuring the code so it sorts $mailboxes by the one primary sort (dirname) and where to put the code. Thanks!! Quote Link to comment Share on other sites More sharing options...
MMDE Posted December 10, 2010 Share Posted December 10, 2010 $array[$dirname][]=$filename; $keyarray[]=$dirname; ^ when it gets the dirname and filename (in the loop) v after when you want it all into one big array. sort($keyarray); for($i=0;$i<count($keyarray);$i++){ for($i2=0;$i2<count($array[$i]);$i2++){ $finalarray[]=$array[$i][$i2]; } } or do you not want it all in the end into one big array? =o 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.