[英]How to put the answer from recursive function in an array (PHP)?
那里,
我多次嘗試將遞歸函數的答案放入數組 ($result) 中,但我不能。 這里的代碼:
function readDirs($path , $result = [])
{
$dirHandle = opendir($path);
while($item = readdir($dirHandle))
{
$newPath = $path."/".$item;
if(is_dir($newPath) && $item != '.' && $item != '..')
{
readDirs($newPath, $result);
}
elseif(!is_dir($newPath) && $item != '.DS_Store' && $item != '.' && $item != '..')
{
echo "$path<br>";
$result[] = $path;
return $result;
}
}
}
$path = "/Users/mycomputer/Documents/www/Photos_projets";
$results = array();
readDirs($path, $results);
你能幫我把路徑放在數組中嗎,因為我稍后在我的代碼中需要它們? 謝謝
我無法理解您的問題,但可以肯定這可能會有所幫助
function readDirs($path)
{
$result = [];
$dirHandle = opendir($path);
while($item = readdir($dirHandle))
{
$newPath = $path."/".$item;
if(is_dir($newPath) && $item != '.' && $item != '..')
{
$result[] = readDirs($newPath);
}
elseif(!is_dir($newPath) && $item != '.DS_Store' && $item != '.' && $item != '..')
{
echo "$path<br>";
$result[] = $path;
return $result; //im not sure this return statement you implement is needed try it to remove it if this does not work
}
}
return $result;
}
$path = "/Users/mycomputer/Documents/www/Photos_projets";
$result = readDirs($path);
var_dump($result);
嘗試並響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.