Re: Appending into associative arrays
| From: | Zoltán Németh | Date: | Mon, 16 Apr 2007 08:50:09 +0000 |
| Subject: | Re: Appending into associative arrays | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252990@lists.php.net to get a copy of this message | ||
2007. 04. 16, hétfő keltezéssel 10.40-kor Otto Wyss ezt írta:
> Zoltán Németh wrote:
> > what do you mean by doesn't work? what error is thrown if any? what
> > result do you get instead of the expected?
> > at first glance I cannot see anything wrong with your function...
> >
> It simply doesn't add any sub folder to $dirs. Could it be that the
> function doesn't return the $dirs parameter?
yes at second look I see the problem. you should do it this way:
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);
$dirs = recurseDir ($d, true, $dirs);
}
}
}
closedir ($handle);
asort ($dirs);
return $accending? $dirs: array_reverse ($dirs);
}
and then you can call array_keys on the result of the whole recursion
like:
$dirnames = array_keys($base, $accending);
greets
Zoltán Németh
>
> O. Wyss
>