TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Sorting number reversed problem... (http://www.talkphp.com/general/3903-sorting-number-reversed-problem.html)

Peuplarchie 01-21-2009 05:50 PM

Sorting number reversed problem...
 
Good day top you all, I'm working on a script that read a folder a list directory. The files in that folder are named by number, like 0 to 200. here how it would look like : 003 002 001 010 ... How can I make my list in complete reverse order like : 010 ... 003 002 001 here is my code :
PHP Code:

 function listFilesInDir($start_dir)          {                  /*         returns an array of files in $start_dir (not recursive)         */                          $files = array();         $dir opendir($start_dir); $count =0;         while(($myfile readdir($dir)) !== false)                 {                 if($myfile != '.' && $myfile != '..' && !is_file($myfile) && $myfile != 'resource.frk' && !eregi('^Icon',$myfile) )                         {                         $count $count +1;                             $files[] = $myfile;                         }                 }         closedir($dir);         rsort($files);         return $files;                   } 

Thanks !

Wildhoney 01-21-2009 06:49 PM

The easiest, and quite possibly the best way, would be to use the glob function. Please have a read of this article. Once you have that, you simply array_reverse the array, and that gives you them in reverse order. Please take a look at the following code; you would want the descending function:

php Code:
function TalkPHP_Dir_Display_Asc($szGlobExpression)
{
    return glob($szGlobExpression, GLOB_ONLYDIR);
}

function TalkPHP_Dir_Display_Desc($szGlobExpression)
{
    $aDirs = glob($szGlobExpression, GLOB_ONLYDIR);
    return array_reverse($aDirs);
}

$aDirs = TalkPHP_Dir_Display_Desc('./*');
$aDirs = array_map('basename', $aDirs);
echo 'Directories: ' . implode(', ', $aDirs);

Peuplarchie 01-21-2009 08:04 PM

Solved !
PHP Code:

 natsort($files); $files array_reverse($files); 



All times are GMT. The time now is 01:18 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0