RadGH Posted June 30, 2008 Share Posted June 30, 2008 I have an array with a list of files given from a directory using this directoryToArray() function here. And example array would be: $Files[0] = "./files/my_zip.zip" $files[1] = "./files/image.bmp" $Files[2] = "./files/your_zip.zip" I'm need some help sorting this by the extension. If anyone has some other suggestions I'm totally open to them, what I'm trying to do is list any selected folder with Javascript in a table listing the name, extension, size, and date modified. Simple. But I'd like it to be more user friendly for larger folders. I've tried a few things. Simply sorting worked alphabetically, but thats not good enough. Splitting the array into a 2d array with the second element being the extension, and then sorting the second array would have worked but I couldn't find a sufficient way to accomplish that. Also, is there a more efficient way to find the extension than using: $ext = explode("/", $file); $ext = explode(".", $ext[count($ext) - 1]); $ext = $ext[count($ext) - 1]; Link to comment https://forums.phpfreaks.com/topic/112530-best-way-to-sort-directory-array-by-extension/ Share on other sites More sharing options...
thatsgreat2345 Posted June 30, 2008 Share Posted June 30, 2008 <? $Files = array("./files/my_zip.zip", "./files/image.bmp", "./files/your_zip.zip"); foreach ($Files as $v) { $Extensions[] = strrchr($v,'.'); } asort($Extensions); print_r($Extensions); ?> Link to comment https://forums.phpfreaks.com/topic/112530-best-way-to-sort-directory-array-by-extension/#findComment-577816 Share on other sites More sharing options...
RadGH Posted June 30, 2008 Author Share Posted June 30, 2008 Amazing... It's much shorter than everything I tried and works great. All I need was "array_multisort($Extensions, $files);" instead of just asort() and its exacly what I needed! Thanks! Link to comment https://forums.phpfreaks.com/topic/112530-best-way-to-sort-directory-array-by-extension/#findComment-577831 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.