Alex Rivera | Logout

glob() — Sort by Name

Asked 2011-10-10T03:55:32.713
19

How can I reverse the sort by filename? Currently it displays all the text files in alphabetical / numerical order, but I'm trying to have it display in descending order instead. Right now, I have...

<?php  
foreach (glob("*.txt") as $filename) {
   include($filename);
}
?>

I'm pretty new to PHP, but I tried usort with array added on but that just resulted in it displaying only 1 of the text files, so either that doesn't work or I just coded it wrong.

Edit
Report

1 Answer

2

The way to do it with usort() would be...

usort($files, function($a, $b) {
    return strcmp($b, $a);
});

CodePad.

answered 2011-10-10T04:01:54.143

Your Answer