Question : php array ordered oddly

hello I have a simple sctip to just read some files and make a list.. problem is that the order is not right at all .. sort is not working.. and even the order they they are in is random.. In the image you can see the order they should be in..

and this is the output for that same area..

Kooskia to Lewiston
Kamiah
 download links: 128kbps | 256kbps

Milepost 14
 download links: 128kbps | 256kbps

Kooskia
 download links: 128kbps | 256kbps

Orofino
 download links: 128kbps | 256kbps

I don't know what it's thiking but it seem to be just random.. :/ not good.. Ideas on how to fix?  Thanks for the help.. Cheers -Jeremy
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
global $gCms;
$config =& $gCms->GetConfig(); 

if(isset($_GET['file']) && $_GET['file']!=''){
	$file='http://www.visitnorthcentralidaho.org/'.$_GET['file'];
	header('Content-Description: File Transfer');
	header('Content-Type: application/force-download');
	header('Content-Disposition: attachment; filename='.basename($file));readfile($file);
}else{


foreach (new DirectoryIterator('uploads/NCITA-Audio-Tour/Northwest-passage-scenic-byway/128kbps') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    $fname=$fileInfo->getFilename();
    $filename= str_replace('---',' & ',$fname); 
    $filename= str_replace('-',' ',$filename); 
    echo  $filename. "<br>";
	if($fileInfo->isDir()){
	$subdir=new DirectoryIterator('uploads/NCITA-Audio-Tour/Northwest-passage-scenic-byway/128kbps/'.$fname);
	asort ($subdir,SORT_NUMERIC);
		foreach ($subdir as $filesInfo) {
			if($filesInfo->isDot()) continue;
				$fname=$filesInfo->getFilename();
				$folder128='uploads/NCITA-Audio-Tour/Northwest-passage-scenic-byway/128kbps/'.$fname.'/';
				$folder256='uploads/NCITA-Audio-Tour/Northwest-passage-scenic-byway/256kbps/'.$fname.'/';
				$filename=basename($filesInfo->getFilename());
				$filename= substr($filename, 0, -4); 
				$filename= substr($filename, 2); 
				$filename= str_replace('---',' &amp; ',$filename); 
				$filename= str_replace('-',' ',$filename); 
				echo $filename.' <br/>';
				echo "<span class='mp3' style='color:#EAE5D2;'>$folder128$fname</span>";
				echo "<span>download links: </span><a href='http://www.visitnorthcentralidaho.org/audio-tours.html?showtemplate=false&file=$folder128$fname' >128kbps</a> | <a href='http://www.visitnorthcentralidaho.org/audio-tours.html?showtemplate=false&file=$folder256$fname' >256kbps</a><br/><br/>";
		}
		
	}
}

}
Attachments:
 
 

Answer : php array ordered oddly

sort and asort sort arrays, but DirectoryIterator is not an array, despite providing array-like access (e.g. via foreach).  Try creating an associative array from the DirectoryIterator results and sorting that, something like:

$dircontents = array();
foreach ($subdir as $filesInfo) {
  $dircontents[$filesInfo->getFilename()] = $filesInfo;
}

sort($dircontents);

foreach($dircontents as $filename => $filesInfo) {
  ...
}

Random Solutions  
 
programming4us programming4us