Re: sorting readdir output? [message #184007 is a reply to message #183997] |
Sun, 01 December 2013 23:41 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On Sun, 01 Dec 2013 11:39:20 -0500, richard wrote:
> <?php
>
> if ($handle = opendir('../audio/1960/')) {
> echo "Directory handle: $handle\n";
> echo "Entries:\n";
>
> /* This is the correct way to loop over the directory. */
> while (false !== ($entry = readdir($handle))) {
> echo "$entry\n<br>";
Make sure here that you use the form of break tag that matches your
doctype.
> }
>
> closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?
>
> http://us1.php.net/readdir
What array? At the moment you're working with a directory handle, from
which you're reading one filename at a time. They come in whatever order
the underlying os code delivers them.
If you want them sorted, read them in to an array and sort it, and then
output the array in order, but at the moment, in your code, there is no
array.
Without testing, the following might work:
<?php
$files = array();
if ( $handle = opendir ( '../audio/1960/' ) ) while ( false !== ( $entry
= readdir( $handle ) ) ) $files[] = $entry;
sort( $files );
foreach ( $files as $file ) echo "{$file}<br>\n";
?>
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|