Search code examples
phpcounterglobreaddiropendir

Glob and counter in php


I have this script which arranges my folders in some tags and takes only 4 items.. which is great! But I need it to take the items by the alphabet

    $counter = 0;
$directory = opendir("albums/");  
while (($item = readdir($directory)) !== false && $counter < 4) { 
    if (($item != ".") && ($item != "..")){ 
    $files[] = $item;
    //..
    $counter++;
    } 
} 

I have this script and I would like to combine or replace it with

$items = glob("albums/*", GLOB_ONLYDIR);
{
    foreach($items as $item)
    {
    //..
    }
}

How can i do it? Glob function is new to me...


Solution

  • To take the 4 first items

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

    array_slice()