Reputation: 46463
Let's say I have these files in the current folder:
a.txt
b.txtbis
c.txt
+ other files with other extensions
I want to list .txt
and .txtbis
files in a single list with PHP glob
, and I want the result to be sorted by filename. Unfortunately:
glob("*.{txtbis,txt}", GLOB_BRACE)
gives Array ( [0] => b.txtbis [1] => a.txt [2] => c.txt )
glob("*.{txt,txtbis}", GLOB_BRACE)
gives Array ( [0] => a.txt [1] => c.txt [2] => b.txtbis )
None of them is sorted as it should.
How to have a list sorted by filename when using braces in glob
?
Upvotes: 0
Views: 3990
Reputation: 7703
Glob sorts the files by full path names (path + name + extension) alphabetically. If you only want to sort by (base)name, you can use usort:
$files = glob("*.{txtbis,txt}", GLOB_BRACE);
usort(
$files,
function($a,$b){
return basename($a) <=> basename($b);
}
);
var_dump($files);
Upvotes: 1