【发布时间】:2010-02-28 21:12:50
【问题描述】:
我有一个函数可以从我的数据库中获取父母的所有孩子的 ID。所以,如果我查找 id 7,它可能会返回一个包含 5、6 和 10 的数组。然后我想要做的是递归地找到这些返回的 id 的孩子,依此类推,直到孩子的最终深度。
我试图编写一个函数来执行此操作,但我对递归感到困惑。
function getChildren($parent_id) {
$tree = Array();
$tree_string;
if (!empty($parent_id)) {
// getOneLevel() returns a one-dimentional array of child ids
$tree = $this->getOneLevel($parent_id);
foreach ($tree as $key => $val) {
$ids = $this->getChildren($val);
array_push($tree, $ids);
//$tree[] = $this->getChildren($val);
$tree_string .= implode(',', $tree);
}
return $tree_string;
} else {
return $tree;
}
}//end getChildren()
函数运行后,我希望它返回一个包含所有找到的子 ID 的一维数组。
【问题讨论】:
标签: php recursion parent-child children