Return the first file from each sub-directory
<p>I've managed to create a function that scans a directory and sub-directories and then merges them into an array.</p>

<p>I can get this to output nicely but what I really want is for it to only output the first file from the sub directories</p>

<pre><code>function getFiles($directory) {
    if($dir = opendir($directory)) {
        $tmp = Array();
        while($file = readdir($dir)) {
            if($file != "." &amp;&amp; $file != ".." &amp;&amp; $file[0] != '.') {
                if(is_dir($directory . "/" . $file)) {
                    $tmp2 = getFiles($directory . "/" . $file);
                    if(is_array($tmp2)) {
                        $tmp = array_merge($tmp, $tmp2);
                    }
                } else {
                    array_push($tmp, $directory . "/" . $file);
                }
            }
        }
        closedir($dir);
        return $tmp;
    }
}

$theFiles = getFiles($_SERVER['DOCUMENT_ROOT']."/images/gallery");

sort($theFiles);

foreach ($theFiles as $v){
    echo "&lt;img src=".$v." /&gt;";
}
</code></pre>
