My php script outputs an unsorted array as shown below. However, I need the file names sorted numerically in ascending order. Please provide the necessary code for outputting these files in a numerically sorted array. Thanks (I'm a beginner).
Unsorted output of my php script:
var galleryarray=new Array();
galleryarray[0]="Flower4.jpg";
galleryarray[1]="Flower5.jpg";
galleryarray[2]="Flower1.jpg";
galleryarray[3]="Flower2.jpg";
galleryarray[4]="Flower3.jpg";
Here's the php script that generated the above unsorted array:
<?php
//PHP SCRIPT: getimages.php
Header("content-type: application/x-javascript");
//This function gets the file names of all images in the current directory
//and outputs them as a JavaScript array
function returnimages($dirname=".") {
$pattern="/Flower*/"; //valid file name
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if (preg_match($pattern, $file)){ //if this file has a valid name
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$file .'";';
$curimage++;
}
}
closedir($handle);
}
return($files);
}
echo 'var galleryarray=new Array();'; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>