DirectoryIterator question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wlbragg
Forum Newbie
Posts: 8
Joined: Fri Jan 25, 2008 12:40 am
Location: Kansas, US

DirectoryIterator question

Post 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?
wlbragg
Forum Newbie
Posts: 8
Joined: Fri Jan 25, 2008 12:40 am
Location: Kansas, US

Re: DirectoryIterator question

Post 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.
Post Reply