[英]Sorting an array of directory filenames in descending natural order
我有一個按自然降序返回的內容目錄。
我正在使用scandir()
和natsort()
,但添加array_reverse()
沒有產生任何結果。
我一直在研究使用opendir()
和readdir()
的組合以及其他什么會影響這個結果。
要排序的項目是編號的圖像文件。 它們將被返回為: 10
9
8
7
等等,但就像從1000
999
998
997
... 直到0
這是我當前的代碼:
$dir = 'dead_dir/dead_content/';
$launcher = scandir($dir);
natsort($launcher);
array_reverse($launcher, false);
foreach ($launcher as $value) {
if (in_array(pathinfo($value, PATHINFO_EXTENSION), array('png'))) {
echo '<img src="dead_dir/dead_content/'.$value.'" />'
}
}
問題很簡單。 array_reverse()
不通過引用修改。 您混淆了sort()
ing 函數的行為。 相反,您只需要使用它生成的返回值。 也就是說,有一個更好的方法。 繼續閱讀...
從 PHP5.4 開始, rsort($array, SORT_NATURAL)
將按 DESC 順序對數組進行排序,並將連續數字視為數字而不是字符串。 與natsort()
ing 然后array_reverse()
ing相比,這是一種更直接和簡潔的方式技術。
(演示)
(帶有.jpg
擴展名的演示)
(數字前帶有靜態字符串的演示)
我建議使用glob()
來掃描您的目錄。 這樣您就可以在同一個調用中添加.png
過濾器。
$dir = 'dead_dir/dead_content/';
$filepaths = glob($dir . '*.png');
rsort($filepaths, SORT_NATURAL);
foreach ($filepaths as $filepath) {
echo '<img src="' . $filepath . '" />';
}
如果你寧願忽略的路徑,並只返回文件名,只是改變你的C urrent w ^ d工作會有irectory。
chdir('dead_dir/dead_content');
$filenames = glob('*.png');
rsort($filenames, SORT_NATURAL);
foreach ($filenames as $filename) {
echo "<div>.png filename => $filename</div>";
}
現在,如果您需要對文件名進行更專業的處理,那么自定義排序功能可能會很好地為您服務。
如以下代碼段所示,飛船操作員會自動神奇地將rsort()
數字字符串輸入為整數,並給出與上述rsort()
解決方案相同的結果。 使用飛船運營商仍比使用更直接的natsort()
然后array_reverse()
代碼:(演示)
$filenames = ["10", "1", "100", "1000", "20", "200", "2"];
usort($filenames, function($a, $b) {
return $b <=> $a;
});
var_export($filenames);
輸出:
array (
0 => '1000',
1 => '200',
2 => '100',
3 => '20',
4 => '10',
5 => '2',
6 => '1',
)
如果您的文件名包含前導或尾隨非數字字符,您可以在usort()
內部進行比較時執行必要的操作以去除不需要的字符。
如果有人不熟悉自定義排序如何與飛船操作員一起工作......
$a <=> $b
。$b <=> $a
。 $dir='dead_dir/dead_content/';
$launcher= scandir($dir);
natsort($launcher);
$r_launcher = array_reverse($launcher,true);
foreach($r_launcher as $value ){
if(in_array(pathinfo($value, PATHINFO_EXTENSION),array('png'))){
echo '<img src="dead_dir/dead_content/'.$value.'" />'}}
如果您的圖像名稱的格式為123-image_name.jpg
、 2323-image_name.jpg
,...這將執行以下操作:
/**
* Compares digits in image names in the format "123-image_name.jpg"
*
* @param string $img1 First image name
* @param string $img2 Second image name
* @return integer -1 If first image name digit is greater than second one.
* 0 If image name digits are equal.
* 1 If first image name digit is smaller than second one.
*/
function compareImageNames($img1, $img2){
$ptr = '/^(\d+)-/'; // pattern
// let's get the number out of the image names
if (preg_match($ptr, $img1, $m1) && preg_match($ptr, $img2, $m2)) {
$first = (int) $m1[0]; // first match integer
$second = (int) $m2[0]; // second match integer
// equal don't change places
if($first === $second) return 0;
// if move first down if it is lower than second
return ($first < $second) ? 1 : -1;
} else{
// image names didn't have a digit in them
// move them to front
return 1;
}
}
// sort array
usort($images, 'compareImageNames');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.