1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
該sliceTree()函數(shù)基本上會(huì)查找某個(gè)確定id值并將其返回。像這樣的東西:
function sliceTree($tree, $branchId)
{
// check all branches
foreach ($tree as $branch) {
// have we found the correct branch?
if ($branch['id'] == $branchId) return $branch;
// check the children
if (isset($branch['children'])) {
$slice = sliceTree($branch['children'], $branchId);
if (isset($slice)) return $slice;
}
}
// nothing was found
return null;
}
如您所見,該例程是遞歸的。代碼未經(jīng)測(cè)試。
我為混合的隱喻感到抱歉:分支機(jī)構(gòu)和子級(jí),但是您是從頭開始的。
此功能比我希望的要復(fù)雜一些,因?yàn)樵谀氖纠?,children當(dāng)沒有子代時(shí)該鍵不存在。我通常希望它在那里并且該值是一個(gè)空數(shù)組。
- 1 回答
- 0 關(guān)注
- 177 瀏覽
添加回答
舉報(bào)