Home > Back-end >  PHP - sort by numerical order
PHP - sort by numerical order

Time:09-17

Im trying to sort the items inside /folders by numerical order but they are ordered like this: enter image description here

 foreach(glob('C:\xampp\htdocs\myweb\central/folders/*', GLOB_ONLYDIR) as $dir) {
    $dir = basename($dir);
    $button = "<button class='items' >$dir</a></button>";
    echo $button;
    
}

Thank y'all in advance.

CodePudding user response:

You get the files/directories as the filesystem sorts them. You need a natural sort:

$directories = glob('C:\xampp\htdocs\myweb\central/folders/*', GLOB_ONLYDIR);
natsort($directories);
//or
//sort($directories, SORT_NATURAL);

//foreach($directories as $dir)
  • Related