我从家庭NAS FTP服务器返回了此阵列:
$arr = array (
0 => 'FOLDER./Vacation.Paris.2018.5Month-Kodak[town]',
1 => './Vacation.Paris.2018.5Month-Kodak[town]/DESC.txt',
2 => './Vacation.Paris.2018.5Month-Kodak[town]/NUMBERS_CONTACT.vbs',
3 => './Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.mkv',
4 => './Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.dbs',
5 => 'FOLDER./Vacation.Dubai.2018-Kodak',
6 => './Vacation.Dubai.2018-Kodak/DESC.txt',
7 => './Vacation.Dubai.2018-Kodak/NUMBERS_CONTACT.vbs',
8 => 'FOLDER./Vacation.Dubai.2018-Kodak/Familly',
9 => './Vacation.Dubai.2018-Kodak/Subs/2_IMG00536.jpg',
10 => './Vacation.Dubai.2018-Kodak/Subs/3_IMG00537.jpg',
11 => './Vacation.Paris.2018.5Month-Kodak[town]/Vacation.Dubai.2018.Center-Kodak.mp4',
12 => './Vacation.2019.5Month.Sweeden-Kodak.mp4',
);
如您所见,我休假时有很多文件夹,现在我正在写html / css / jquery相册,但是我需要以php正确的顺序首先获取上面的输出:
[0] => FOLDER./Vacation.Paris.2018.5Month-Kodak[town]
[1] => ./Vacation.Paris.2018.5Month-Kodak[town]/DESC.txt
[2] => ./Vacation.Paris.2018.5Month-Kodak[town]/NUMBERS_CONTACT.vbs
[3] => ./Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.mkv
[4] => ./Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.dbs
[5] => ./Vacation.Paris.2018.5Month-Kodak[town]/Vacation.Dubai.2018.Center-Kodak.mp4
[6] => FOLDER./Vacation.Dubai.2018-Kodak
[7] => ./Vacation.Dubai.2018-Kodak/DESC.txt
[8] => ./Vacation.Dubai.2018-Kodak/NUMBERS_CONTACT.vbs
[9] => FOLDER./Vacation.Dubai.2018-Kodak/Familly
[10] => ./Vacation.Dubai.2018-Kodak/Subs/2_IMG00536.jpg
[11] => ./Vacation.Dubai.2018-Kodak/Subs/3_IMG00537.jpg
[12] => ./Vacation.2019.5Month.Sweeden-Kodak.mp4
这是该论坛成员尝试的代码,但无法正常工作:
因此,如果您可以帮助我修改上面的代码,以便获得所需的输出。
谢谢
答案 0 :(得分:1)
这使用usort
来完成工作,并且需要花些时间才能获得以后的排序顺序。
第一部分是从字符串中提取年份-假设它类似于xxxxx 2020 xxxxx
,这就是为什么它使用此处使用的正则表达式中的第二个元素的原因。然后,它将仅从字符串的开头删除FOLDER,以确保不会在其他任何地方删除它。然后,为了获得正确的订单,它使用年份,后跟已处理的字符串...
usort($arr, function($a, $b) {
$f = "FOLDER";
if ( substr( $a, 0, strlen($f)) == $f ) {
$a = substr( $a, strlen($f));
}
$y1 = preg_split("/\D+/", $a)[1];
if ( substr( $b, 0, strlen($f)) == $f ) {
$b = substr( $b, strlen($f));
}
$y2 = preg_split("/\D+/", $b)[1];
return $y1.$a <=> $y2.$b;
});
print_r($arr);
此操作使用飞船运算符进行比较,该操作仅适用于PHP 7 +。