Page 1 of 1

DirectoryIterator question

Posted: Tue Mar 18, 2008 1:46 pm
by wlbragg
Does anyone know how to sort the return values from DirectoryIterator without first putting it in an array?

Code: Select all

 
$days=funcToCheckHistory();
 
for ($i=1,$daysGoingBack=$days;$i<(($days-1)+2);$i++,$$daysGoingBack--) {
     $dataDir =                                           
          $filepath.date('Ymd', mktime(0, 0, 0, 
          date("m"), 
          date("d")-$daysGoingBack, 
          date("y"))).$fpath;
 
     $dir = new DirectoryIterator($dataDir);
        
     foreach ($dir as $file) {
          if (!$dir->isDot()) {
                $comp = date($file->getMTime());
                // do something with $comp
                // I could put it in an array here
                array[]= $comp;
          }
     }
}
 
$comp DOES NOT necessary come out sorted by modified time. For the most part it does but not always for some unknown (to me) reason. I know I could store $comp in an array and sort the array but is there a way to sort the "foreach ($dir as $file)" part of the code?

Re: DirectoryIterator question

Posted: Tue Mar 18, 2008 6:25 pm
by wlbragg
I answered my own question, I guess. In case anyone else wants to know.

Code: Select all

 
     $dir = new DirectoryIterator($dataDir);
     sort($dir)
     foreach ($dir as $file) {.....}
 
Sorry for asking such a basic question. I have never used any sort functions and also didn't understand that $dir = new DirectoryIterator($dataDir) was creating an array in $dir. If I am missing something or incorrect about any of this please let me know.