简体   繁体   中英

Order by date extracted from filename

I am trying to order the result by year, the year extracted from the filename.

These are the N files in the folder I am scanning:

filename_2014.jpg
filename_2013.jpg
filename_2012.jpg
filename_2011.jpg
....
....

So I have wrote this function:

function Archivize() {
    $path = opendir('./');

    while($read = readdir($path)) {
            if($read != '.' && $read != '..') {
                $filename = current(explode(".", $read));
                $getYear = substr($filename, -4);
                ?>
                     <span class="year"><?php echo $getYear ?></span>
                <?php
            }
    }
     closedir($path);
}

It basically works, but I do not know how to order the results by the year extracted from the filename.

I read that the best way is to use an array and then sort(), but I really can't figure out how to apply this hint to my function.

function Archivize() {
    $path = opendir('./');

    $filesArray = array(); //just defining the array

    if ($handle = $path) {
        $loop = 1;
        while($read = readdir($path)) {
            if($read != '.' && $read != '..') {
                $filename = current(explode(".", $read));
                $getYear = substr($filename, -4);
                ?>
                    <span class="year"><?php echo $getYear ?></span>
                <?php
                echo "file:$read<br/>";
                $filesArray[] = $getYear;   //add the file into the files array
                $loop++;
            }
        }
            closedir($path);
    }
}

Probably the second function with the array is an aberration, but I am crawling in the dark.

UPDATE:

Actually the array is printed as:

filename_2011.jpg
filename_2012.jpg
filename_2013.jpg
filename_2014.jpg

I want to reverse it! Is it possible?

Have a look at the asort function and associative arrays. For your case, you'd want to Use the filename as the key, and your extracted year as the value.

you can store them in array and sort array using asort and print like this

function Archivize() {
    $path = opendir('./');

    $filesArray = array(); //just defining the array

    if ($handle = $path) {
        $loop = 1;
        while($read = readdir($path)) {
            if($read != '.' && $read != '..') {
                $filename = current(explode(".", $read));
                $getYear = substr($filename, -4);

                echo "file:$read<br/>";
                $filesArray[] = $getYear;   //add the file into the files array
                $loop++;
            }
        }

       asort($filesArray);
       foreach($filesArray as $n_year)
       {
       ?>
            <span class="year"><?php echo $n_year?></span><br/>
       <?php
       } 
            closedir($path);
    }
}

You will need 2 loops.

One for adding files to the array and another to write the contents to screen.

$path = opendir('./');
$list = array()
while($read = readdir($path)){
    if(substr($read, 0, 1) == '.')
        continue;

    $year = substr(basename($read, ".jpg"), -4);
    $list[$year] = $read;
}
closedir($path);

krsort($list); //using krsort to sort by keys (the years from most recent to older in this case)

foreach($list as $year => $filename){
    ?><span class="year"><?php echo $year ?></span><?php echo $filename;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM