Sorting arrays in php - then back to Flash

Hello everyone. I have a problem that I can’t seem to solve. I have a completely dynamic imageviewer that creates a specific number of buttons based on how many folders are in a directory. I have php script that does this. I can’t figure out how to arrange the array in alphabetical order before it is sent back to Flash. I ahve also tried sorting in Flash and have been unsuccessful. Here’s is the script I have so far which is way off I know. Can someone give me a hand with this? Thanks.

$dir = "Gigs";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if($filename != "." && $filename != "..") {



foreach($foldernames as $val) {
 echo  "&foldernames=".$val;

Here’s a link to the image viewer: