【发布时间】:2024-01-15 03:04:01
【问题描述】:
我正在尝试按层次顺序打印类别,无论它们有多深,我一直在尝试但仅在第二级成功,如果它们是 3 或 4 级深度怎么办。我希望他们在下拉列表中打印
Tasks
-hard task
--simple task
Notes
-hard note
--simple note
---easy note
记录
$records = array(
array( 'id'=>'1', 'parent'=>'0', 'name'=>'Tasks' ),
array( 'id'=>'2', 'parent'=>'0', 'name'=>'Notes' ),
array( 'id'=>'3', 'parent'=>'1', 'name'=>'hard task' ),
array( 'id'=>'4', 'parent'=>'3', 'name'=>'simple task' ),
array( 'id'=>'5', 'parent'=>'2', 'name'=>'hard note' ),
array( 'id'=>'6', 'parent'=>'5', 'name'=>'simple note' ),
array( 'id'=>'7', 'parent'=>'6', 'name'=>'easy note' ),
);
我正在尝试的代码
function print_records($records){
foreach ($records as $rec){
echo $rec['name'];
$get_parent = $rec['parent'];
get_sub_cat($get_parent);
}
}
function get_sub_cat($get_parent){
foreach ($get_parent as $rec){
echo $rec['name'];
$get_sub = $rec['parent'];
get_sub_child_cat($get_parent);
}
}
在这里我完全迷路了!我见过递归,但不知道在这种情况下如何使用
【问题讨论】: