Re: Appending into associative arrays

From: Date: Mon, 16 Apr 2007 07:27:15 +0000
Subject: Re: Appending into associative arrays
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-252986@lists.php.net to get a copy of this message
Alister Bulman wrote:
$dirs[$d] = filemtime($d);
Has he even retrieved the directories in sorted order by modification time? If not he still needs to sort.
Then he'll need an asort($dirs); They would not have come in any particular order, so you have to sort them for whatever you need anyway. Fine. But how do I now implement recursive looking for directories? My code doesn't work.
function recurseDir ($base, $accending = true, $dirs = array()) {
    $handle = opendir ($base);
    while ($dir = readdir($handle)) {
      if (($dir != '..') and ($dir != '.')) {
        $d = $base.'/'.$dir;
        if (is_dir ($d)) {
          $dirs[$d] = filemtime($d);
          recurseDir ($d, true, $dirs);
        }
      }
    }
    closedir ($handle);
    asort ($dirs);
    return array_keys ($accending? $dirs: array_reverse ($dirs));
} O. Wyss

« previous php.general (#252986) next »