所以,我真的花了兩天的時間試圖讓我的腦海中。我想要一個從dirtree創建多維數組的函數。我手動之前做到了,得到了這樣的結果:獲取多維子目錄數組的遞歸函數
Array()
[cats] = Array()
[domestic] = Array()
[wild] = Array()
[dogs] = Array()
[big] = Array()
[small] = Array()
[emptyfolder]
通過手動我的意思是做我的函數中是這樣的:$result['level1']['level2'] = array();
這不能很好地工作,因爲我不知道dirtree的深度事前。
功能還使用另一個函數GetSubdirectories($directory)
返回和數組像這樣:
[0] = 'cats',
[1] = 'dogs'
[2] = 'emptyfolder'
所以,這裏是有問題的功能:
<?php
public function GetStructure($directory, $node = 'content') //the root folder is content
{
if (!isset($structure))
{
$structure = array();
}
$subdirectories = $this->GetSubdirectories($directory);
if (is_array($subdirectories))
{
foreach ($subdirectories as $subdirectory)
{
//add new subarray
$structure[$node] = array();
//set the current path
$currentDirectory = $directory . "/" . $subdirectory;
//repeat
$this->GetStructure(); // <-- how to I call the function?
}
}
return $structure;
}
?>
從我的理解,問題是「告訴功能放置下一個陣列的位置「。我只是不明白,任何幫助將不勝感激!
SOLUTION
<?php
public function PopulateArrayWithDirtree($dir, &$array = false)
{
$subDirs = $this->GetSubdirectories($dir);
if (is_array($subDirs))
{
for ($i = 0; $i < sizeof($subDirs); $i++)
{
$array[$subDirs[$i]] = array();
$this->PopulateArrayWithDirtree(($dir . "/" . $subDirs[$i]), $array[$subDirs[$i]]); //repeat
}
}
return true;
}
public function Test()
{
$path = $this->contentpath;
$structure = array();
$this->PopulateArrayWithDirtree($path, $structure);
print_r($structure);
}
?>
感謝,但不知何故,這仍然只返回dirtree的第一級.. – user1293977 2012-08-16 23:17:48
好嗎,你讓我走在正確的道路上。請參閱編輯解決方案,謝謝! – user1293977 2012-08-17 09:31:42