我想从 PHP 函数返回多个值,但它没有像以下代码一样工作:
该函数用于搜索特定文件夹及其递归文件夹中的文件名,并将文件名存储在数组中。
在此示例中,调用特定(主)文件夹:F:\test
调用递归文件夹:F:\test\subfolder
主文件夹和子文件夹共7个文件,文件名格式为:
主文件夹:1.txt、2.txt、3.txt、4.txt
子文件夹:5.txt、6.txt、7.txt
function getDirectory( $path = '.', $level = 0 ) {
$i=0;$j=0;
$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory, this is what makes function recursive.
} else {
if ($level>0) //in a recursive folder
{
$dir_matched[$j]=$file;
$j++;
}
else //in main folder
{
$files_matched[$i] = $file;
$i++;
}
}
}
closedir( $dh );
//print_r ($files_matched);
//print_r ($dir_matched); I tested this before return, both works fine.
return array($files_matched,$dir_matched);
}
echo "<pre>";
list($a,$b) = getDirectory("F:\test");
print_r ($a); // this will result the same as array $files_matched, it ok!
print_r ($b); // but i don't know why I cannot get the array of $dir_matched??
echo "</pre>";
如您所见,我只能得到一个数组,这太奇怪了?有什么想法可以获取$dir_matched
数组的内容吗?