Search code examples
phparraysnumbersglob

Glob array by number value


I'm using glob to array my sub directories

<?php
$items = glob("../albums/*", GLOB_ONLYDIR);
foreach($items as $item) {
    echo "$item\n ";
}
?>

he orders them by the alphabet, i'd like to order them by numbers

if i got sub-directories named 1 , 3 , 5 , 10

the will be arranged like this : 10, 1, 3, 5

i want them to be like this 1, 3, 5, 10

is there an option? thanks

EDIT: now i found natsort($files); and when im using it with:

$items = array_slice(glob('albums/*', GLOB_ONLYDIR), 0, 4);
natsort($items);

and my folders are 995-1000 it gives me this array : 995, 996, 997, 1000


Solution

  • Take a look at natsort()