【发布时间】:2016-06-03 15:36:31
【问题描述】:
我在mysql db中有一个persons表,每个人都有母亲和父亲,所以我需要建立继承树,我有一个函数,如果只是运行它,它工作正常,但我需要定义继承树的深度,我为此做了引用变量,它很好地计算了递归,但还有另一个问题,它只为母线制作树,如果我切换'if'语句它将只构建父树,我该如何制作它工作还是有其他方法可以做到?
public function makeTree(array &$rootPerson, &$quantity = 14)
{
if ($quantity < 0) {
return;
}
if (isset($rootPerson['mother_id'])) {
$rootPerson['parents']['mother'] = $this->getPerson($rootPerson['mother_id']);
$this->makeTree($rootPerson['parents']['mother'], --$quantity);
}
if (isset($rootPerson['father_id'])) {
$rootPerson['parents']['father'] = $this->getPerson($rootPerson['father_id']);
$this->makeTree($rootPerson['parents']['father'], --$quantity);
}
}
【问题讨论】: