dsfds2343 2018-08-16 04:55
浏览 53

根据文件的最后两个字符对文件进行排序

I collect the files from a folder using some Smarty code:

{assign var=files value="uploads/Documenten/GPS-Tracks/*.html"|glob}

{if count($files)}
    <ul>
        {foreach from=$files item='file'}
            {assign var='gpstrack_location' value="{$file|cms_escape:'urlpathinfo'}"}
            {if !$file|contains:"index"}
                <li><a href="{$file}">{$file|basename|substr:0:-5}</a></li>
            {/if}
        {/foreach}
    </ul>
{/if}

This gives me a sorted list basedon the first character.

  • Brighton-Gosport_2108
  • Brighton-Littlehampton_2012
  • Brighton-Portmouth-Brighton_2018
  • Bruinisse-Herkingen_2012
  • BurnhamonCrouch_Vlissingen_22aug13
  • Calais-Dieppe_2014
  • Carteret-Dielette_2012
  • Cherbourg-StVaast_2012
  • Clubwedstrijd_17sept11
  • Clubwedstrijd_18sep10
  • Clubwedstrijd_21sep13
  • Cowes-BeauLieu_River_2012
  • Dartmouth-Guernsey_2012
  • Deauville-Honfleur_2012
  • Denemarken_2008
  • Dielette-Alderney_2012
  • Dieppe-Boulogne_2012

What I'd like to achive is a sorted list based on the last two charaters. Is there in Smarty or PHP a way to do this?

Thanks in advance for your help!

UPDATE: I got myself a couple of steps futher :) There remains one warning I'm not able to solve: The code so far:

<?php

    $folder = '<some foldername>';

    $files = array_values(preg_grep('/^([^.])/', scandir($folder, SCANDIR_SORT_ASCENDING)));
    //$files = scandir($folder);

    //print_r($files);

    usort(var_dump($files, function ($a, $b){
        if ($a || $b <> "")
            return substr($b, strlen($b) - 2) - substr($a, strlen($a) - 2);
    }));
?>

The warning I get, and thought to solve with the 'or'-statement:

[

  • 121] => string(29) "Weymouth-Lyme_Reges_2012.html"
  • [122]=> string(24) "Yarmouth-Poole_2012.html" }
  • object(Closure)#1036 (1) {["parameter"]=> array(2) { ["$a"]=> string(10) "" ["$b"]=> string(10) "" } } Warning: usort() expects exactly 2 parameters, 1 given in /Users/name/Sites/install/assets/udt/sort-on-last-characters.php on line 55

Someone able to get me a step further?

  • 写回答

1条回答 默认 最新

  • dtmwnqng38644 2018-08-23 14:32
    关注

    Found a solution.

    <?php
    $files = glob($folder.$file_name);
    $current_year = date('y');
    if (
        usort($files, function($a, $b) {
            $yearA = (int) substr($a, -7); // extension + . + last two characters
            $yearB = (int) substr($b, -7);
            return $yearB - $yearA; // descending, for ascending swap $yearA and $yearB
        })
    ) {
        // $files is now sorted, do your stuff here
        foreach ((array) $files as $file) {
    
            if (substr(basename($file, '.html'), -2) == $current_year) {
                echo basename($file, '.html');
                echo "<br />";
            } else {
                $current_year = $current_year - 1;
    
            }
        }
    
    } else {
        // sorting failed, show error message, exception, etc.
        echo "Files not sorted";
    }    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序