[英]Sort list of sub-directories
I have two questions: 我有两个问题:
1) I'm use the below code to output a list of sub-directories contained in the folder 'issues'. 1)我使用下面的代码输出文件夹“ issues”中包含的子目录列表。 Each sub-directory is a number. 每个子目录都是一个数字。 I'd like to sort that output by increasing value, so 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 20, 21 etc. 我想按增加的值对输出进行排序,所以1、2、3、4、5、6、7、8、9、10、11,... 20、21等。
How can I do this? 我怎样才能做到这一点?
<?php
chdir('issues');
$d = dir(".");
echo "<ul>";
while (false !== ($entry = $d->read()))
{
if (is_dir($entry) && ($entry != '.') && ($entry != 'sponsors') &&
($entry != '..'))
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();
?>
2) On a different part of the same page, I also want to output a link to the latest issue, ie the sub-directory with the highest number. 2)在同一页面的其他部分,我还想输出指向最新发行版的链接,即具有最高编号的子目录。 How would I do that? 我该怎么做?
Thank you! 谢谢!
Use scandir() with array_slice to get a sorted array of the directory contents. 将scandir()与array_slice一起使用可获取目录内容的排序数组。
Use asort for accurate numerical sorting as dossy suggested. 如推荐的那样,使用sort进行准确的数字排序。
$directory = 'issues';
$contents = array_slice(scandir('issues'), 2);
$sub_dirs = array();
foreach($contents as $c){
$path = $directory."/".$c;
if(is_dir($path) && $c !== 'sponsors'){
$sub_dirs[] = $c;
}
}
asort($sub_dirs, SORT_NUMERIC);
$latest = end($sub_dirs);
reset($sub_dirs);
echo '<ul>';
foreach($sub_dirs as $sub){
echo '<li><a href="'.$directory.'/'.$sub.'">'.$sub.'</a></li>';
}
echo '</ul>';
echo '<a href="'.$directory.'/'.$latest.'">'.$latest.'</a>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.