0

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

?>
Jeff
  • 179
  • 10
  • Use the `natsort()` function. – Barmar Sep 30 '20 at 22:26
  • Thanks. I'm a beginner, though, and don't know much about coding yet. If possible, please provide the exact coding. I'm hoping there's an easy way to sort this array, perhaps one that I can understand. – Jeff Sep 30 '20 at 22:45
  • I already closed the question with a link to a similar question that shows how to get all the filenames and sort them. – Barmar Sep 30 '20 at 22:47
  • I hope closing a question doesn't mean that others can't respond to my inquiry. – Jeff Sep 30 '20 at 22:53
  • Yes, that's exactly what it means. If there's already an answer in another question, there's no point in answering it again. – Barmar Sep 30 '20 at 22:54
  • If you don't think the other question really answers your question, update the question to show how it's a different problem, and I'll reopen so someone can provide the answer. – Barmar Sep 30 '20 at 22:54
  • I suppose I better start learning more about the natsort() function! – Jeff Sep 30 '20 at 22:58
  • Just put all the filenames in an array, and call `natsort($array)` – Barmar Sep 30 '20 at 23:02
  • There's no need for a loop to assign each array element. Use `echo 'galleryarray = ' . json_encode($array) . ';';` – Barmar Sep 30 '20 at 23:03
  • Thanks for the suggestions, Barmar. Unfortunately, my lack of coding knowledge is causing me to lose sleep. – Jeff Sep 30 '20 at 23:18
  • You can't expect us to do your coding for you. Show what you tried and we'll help you fix it. – Barmar Sep 30 '20 at 23:30
  • I understand. I'll start researching your solutions tomorrow. Thanks, again, Barmar, for pointing me in the right direction. – Jeff Sep 30 '20 at 23:32

0 Answers0