[英]Natural Order needed for directory list
I am new to PHP and I need assistance making this bit work. 我是PHP新手,需要帮助才能使此工作正常。 I am trying to have the directory folders listed in a PHP page. 我正在尝试在PHP页面中列出目录文件夹。 So far it is working but the list of folders appears in "Standard" order. 到目前为止,它仍然有效,但是文件夹列表以“标准”顺序显示。 I need it in "Natural" order but I cannot make it work. 我需要以“自然”顺序使用它,但无法使其正常工作。
Standard Order: 标准订单:
Natural Order: 自然顺序:
Here is what I have so far: 这是我到目前为止的内容:
<?php
$TheFolder = '';
foreach(glob($TheFolder.'*', GLOB_ONLYDIR) as $dir) {
$dir = str_replace($TheFolder, '', $dir);
echo $dir , "<br>";
//echo $dir;
}
?>
I have been trying to use natsort before but could not figure it out. 我之前一直尝试使用natsort ,但无法弄清楚。
Any help would be very appreciated. 任何帮助将不胜感激。
You can use sort()
function with SORT_NATURAL
flag on; 您可以在SORT_NATURAL
标志的情况下使用sort()
函数;
Like so: 像这样:
$array = [1,10,11,12,4,30];
sort($array, SORT_NATURAL);
print_r($array); // Array ( [0] => 1 [1] => 4 [2] => 10 [3] => 11 [4] => 12 [5] => 30 )
So something like this should work: 所以这样的事情应该工作:
$directoriesArray = glob($TheFolder.'*', GLOB_ONLYDIR);
sort($directoriesArray, SORT_NATURAL);
foreach($directoriesArray as $dir){
//.. str_replace and echo $dir
}
Thanks to Mostafa Mohsen who pointed me in the right direction. 感谢Mostafa Mohsen向我指出了正确的方向。
Taking Mostafa's solution as base, a small modification was needed as it was still throwing an error. 以Mostafa的解决方案为基础,需要进行一些小的修改,因为它仍然会引发错误。
This is the final code, working ok: 这是最终代码,可以正常工作:
<?php
$directoriesArray = glob('*', GLOB_ONLYDIR);
sort($directoriesArray, SORT_NATURAL);
foreach($directoriesArray as $dir){
echo $dir , "<br>";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.