【发布时间】:2019-03-15 15:32:23
【问题描述】:
我想按自然顺序对文件夹中的文件进行排序。 这些文件是:“1-Test1.docx”、“2-Test2.docx”、“3-Test3.docx”、“10-Test10.docx”。
当我使用以下内容时:
for( $i= 0 ; $i <= 4; $i++ ){
$nomfichier = glob("vendor/templates/part3/*.docx");
natsort ($nomfichier);
print_r ($nomfichier);
}
我有:
Array ( [0] => folder1/folder2/1-Test1.docx [2] => folder1/folder2/2-Test2.docx [3] => folder1/folder2/1-Test3.docx [1] => folder1/folder2/10-Test10.docx )
没关系。但是当我尝试使用相同的自然顺序回显每个位置时,它会在“1-Test1.docx”之后给我“10-Test10.docx”。
$position = array_search($nomfichier[$i], $nomfichier);
// echo $nomfichier[$i]. " : ". $position;
给...
folder1/folder2/1-Test1.docx : 0 folder1/folder2/10-Test10.docx : 1 folder1/folder2/2-Test2.docx : 2 folder1/folder2/3-Test3.docx : 3
而我希望得到以下结果:
folder1/folder2/1-Test1.docx : 0 folder1/folder2/2-Test2.docx : 1 folder1/folder2/3-Test3.docx : 2 folder1/folder2/10-Test10.docx : 3
我该怎么做才能让它工作?
谢谢!!
【问题讨论】:
-
您是使用 for 循环还是 foreach 循环打印?
-
我使用 for 循环来打印结果。
标签: php arrays sorting for-loop