【发布时间】:2013-06-10 22:54:04
【问题描述】:
在我的 php 项目中,我有一个名为 Gallery1 的文件夹(路径是 images/gallery1)。其中包含名为 1.jpg、2.jpg、20.jpg、8.jpg 等的图像。我想按升序显示该文件夹中的所有图像(1.jpg、2.jpg、8.jpg、20.jpg ETC)。有人知道吗?
提前致谢
【问题讨论】:
-
这可能会帮助你Sorting
标签: php
在我的 php 项目中,我有一个名为 Gallery1 的文件夹(路径是 images/gallery1)。其中包含名为 1.jpg、2.jpg、20.jpg、8.jpg 等的图像。我想按升序显示该文件夹中的所有图像(1.jpg、2.jpg、8.jpg、20.jpg ETC)。有人知道吗?
提前致谢
【问题讨论】:
标签: php
<?php
// Find all files in that folder
$files = glob('images/gallery1/*');
// Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
natcasesort($files);
// Display images
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}
// ???
// Profit :D
?>
【讨论】:
您可以使用按自然顺序排序的natsort。
来自 PHP.net 的示例
$array1 = array("img12.png", "img10.png", "img2.png", "img1.png");
natsort($array1);
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)
【讨论】: