PHP-在下拉列表中按字母顺序对目录进行排序
如何对目录进行排序以按字母顺序显示下拉列表PHP-在下拉列表中按字母顺序对目录进行排序,php,sorting,directory,Php,Sorting,Directory,如何对目录进行排序以按字母顺序显示下拉列表 <select name=country> <?php $handle=opendir("images/flags"); while (false!==($file = readdir($handle))) { if ($file != "." && $file != "..") { $country = substr($file,0,strpos($file,'.')); echo "<
<select name=country>
<?php
$handle=opendir("images/flags");
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$country = substr($file,0,strpos($file,'.'));
echo "<option value=\"".$file."\"><center>".$country."</center></option>\n";
}
}
closedir($handle);
?>
</select>
使用
默认情况下,排序顺序为字母升序。如果
可选的排序顺序设置为按降序排列,然后
排序顺序是按字母降序排列的。如果设置为
SCANDIR_SORT_NONE则结果未排序
更多阅读
另一种解决方案:
<?php
$dir = "images/flags";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
print_r($files);
rsort($files);
print_r($files);
?>
使用
默认情况下,排序顺序为字母升序。如果
可选的排序顺序设置为按降序排列,然后
排序顺序是按字母降序排列的。如果设置为
SCANDIR_SORT_NONE则结果未排序
更多阅读
另一种解决方案:
<?php
$dir = "images/flags";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
print_r($files);
rsort($files);
print_r($files);
?>
您只需使用:
$ao = new ArrayObject(iterator_to_array(new FilesystemIterator(__DIR__ ."/test", FilesystemIterator::SKIP_DOTS)));
$ao->natsort(); //sort directory
foreach ( $ao as $file ) {
echo $file->getPathname() . PHP_EOL;
}
您可以简单地使用:
$ao = new ArrayObject(iterator_to_array(new FilesystemIterator(__DIR__ ."/test", FilesystemIterator::SKIP_DOTS)));
$ao->natsort(); //sort directory
foreach ( $ao as $file ) {
echo $file->getPathname() . PHP_EOL;
}