Stump of a Muffin

Alphabetize an Array

Reading a directory and putting the files found into an array shouldn’t be such a big deal, right?  How about alphabetizing that array?

function read_a_directory( $DIR )
{
if ( is_dir( $DIR ) )
{
if ( $dh = opendir( $DIR ) )
{
while ( ( $file = readdir( $dh ) ) !== false )
{
if ( $file <> ‘.’ && $file <> ‘..’ )
{
$a[] = strtolower( $file ) ;
$b[] = $file ;
}
}
}
}
if ( is_array( $a ) )
{
//sort( $return );
asort( $a ) ;
foreach( $a AS $k => $v )
{
$return[]    = $b[$k] ;
}
}
return $return;
}

I found myself down this path only to take care of the upper and lower case names given to some files.  Further, some were prefaced with numbers.  This little function did the work.

Comments are closed.