PHP only printing last element of returned array -
i'm trying populate array file names ending in "mp4" recursive function looking through list of sub-directories.
when print elements in array foreach loop before return statement, array displays of elements correctly. however, when create variable return of method , try iterate through again, receive last entry in array.
could caused recursion in loop?
my code follows:
<?php function listfolderfiles($dir){ $array = array(); $ffs = scandir($dir); foreach($ffs $ff){ if($ff != '.' && $ff != '..'){ // adds array. if(substr($ff, -3) == "mp4"){ $array[] = $ff; } // steps next subdirectory. if(is_dir($dir.'/'.$ff)){ listfolderfiles($dir.'/'.$ff); } } } // @ point if insert foreach loop, // of elements display return $array; } // new '$array' variable includes // last entry in array in function $array = listfolderfiles("./ads/"); foreach($array $item){ echo $item."<p>"; } ?>
any appreciated! apologize sloppiness. i'm new php.
thanks in advance!
when recurse subdirectory, need merge results array. otherwise, array contains matching files original directory, matches subdirectories discarded.
function listfolderfiles($dir){ $array = array(); $ffs = scandir($dir); foreach($ffs $ff){ if($ff != '.' && $ff != '..'){ // adds array. if(substr($ff, -3) == "mp4"){ $array[] = $ff; } // steps next subdirectory. if(is_dir($dir.'/'.$ff)){ $array = array_merge($array, listfolderfiles($dir.'/'.$ff)); } } } // @ point if insert foreach loop, // of elements display return $array; }
Comments
Post a Comment