mercredi 6 mai 2015

Find all files in a folder and group by part of the filename

I have a folder containing images like so:

  • colorwaves-1.png
  • colorwaves-2.png
  • snow-1.png
  • snow-2.png

I need to populate a drop-down list that contains only:

  • colorwaves
  • snow

this is my code:

<?php
$files = scandir('../resources/banner/');
foreach ($files as $file)
  if ($file != "." && $file != ".."){
  $x = array_shift(explode('-', $file));  //remove everything after "-"
  echo "<option value='".$x."'>".$x."</option>";
}
?>

This will give me of course 4 results with two duplicates. I cant find a way to filter the results before I start the loop. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire