Re: sorting readdir output? [message #184006 is a reply to message #184003] |
Sun, 01 December 2013 18:19 |
Mr Oldies
Messages: 241 Registered: October 2013
Karma:
|
Senior Member |
|
|
On Sun, 1 Dec 2013 18:10:09 -0000, James Harris wrote:
> "Lew Pitcher" <lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote in message
> news:vOKmu(dot)376035$jg4(dot)217970(at)fx05(dot)iad...
>
> ...
>
>>> if ($handle = opendir('../audio/1960/')) {
>
> ...
>
>>> while (false !== ($entry = readdir($handle))) {
>>> echo "$entry\n<br>";
>>> }
>
> ...
>
>>> This gives the output in an unsorted list.
>>> How can I make it so the array is sorted?
>
> ...
>
>> As you read the directory, put each entry into the same array.
>> Once you've hit the end, sort the array.
>
> How about using scandir?
>
> $ents = scandir($document_root . $dir);
> natcasesort($ents);
>
> James
This one does what I want nicely.
thanks.
<?php
$dir = "/tmp";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
print_r($files);
rsort($files);
print_r($files);
?>
|
|
|