您尚未说明父ID:
function categoryTree($permissionRecord, $parent_id = 0, $sub_mark = '')
{
foreach($permissionRecord as $row) {
if ($row->parentid == $parent_id) {
echo $sub_mark.$row->name;
$this->categoryTree($permissionRecord, $row->id, $sub_mark.'---');
}
}
}
尝试一下,看看会发生什么-注意,我已经将递归函数调用包装在
if()
检查当前记录的parentId是否等于传递给方法的父ID。
要使其在缩进列表中显示项目,请执行以下操作:
public function categoryTree($permissionRecord, $parent_id = 0)
{
$html = '<ul>';
foreach($permissionRecord as $row) {
if ($row->parentid == $parent_id) {
$html .= '<li>' . $row->menuname;
$html .= $this->categoryTree($permissionRecord, $row->id, $html);
$html .= '</li>';
}
}
$html .= '</ul>';
return $html;
}
然后打电话:
$html = $this->categoryTree($permissionRecord);
使用单元测试的it示例