You have to split up the array sort the files and directories separately since the dir names are in the keys and the file names are in the values.
$dir_map = dir_map_sort(directory_map('folder/name'));
/**
* Sorts the return of directory_map() alphabetically
* directories listed before files
*
* Example:
* a_dir/
* b_dir/
* a_file.dat
* b_file.dat
*/
function dir_map_sort($array)
{
$dirs = array();
$files = array();
foreach ($array as $key => $val)
{
if (is_array($val)) // if is dir
{
// run dir array through function to sort subdirs and files
// unless it's empty
$dirs[$key] = (!empty($array)) ? dir_map_sort($val) : $val;
}
else
{
$files[$key] = $val;
}
}
ksort($dirs); // sort by key (dir name)
asort($files); // sort by value (file name)
// put the sorted arrays back together
// swap $dirs and $files if you'd rather have files listed first
return array_merge($dirs, $files);
}
OR
/**
* Sorts the return of directory_map() alphabetically
* with directories and files mixed
*
* Example:
* a_dir/
* a_file.dat
* b_dir/
* b_file.dat
*/
function dir_map_sort($array)
{
$items = array();
foreach ($array as $key => $val)
{
if (is_array($val)) // if is dir
{
// run dir array through function to sort subdirs and files
// unless it's empty
$items[$key] = (!empty($array)) ? dir_map_sort($val) : $val;
}
else
{
$items[$val] = $val;
}
}
ksort($items); // sort by key
return $items;
}