递归实现无限极分类
array(8) {
[0] => array(7) {
["jc_id"] => string(1) "2"
["jc_name"] => string(20) "销售/客服/市场"
["jc_parent_id"] => string(1) "0"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "0"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
[1] => array(7) {
["jc_id"] => string(1) "3"
["jc_name"] => string(12) "销售业务"
["jc_parent_id"] => string(1) "2"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "1"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
[2] => array(7) {
["jc_id"] => string(1) "4"
["jc_name"] => string(12) "销售代表"
["jc_parent_id"] => string(1) "3"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "2"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
[3] => array(7) {
["jc_id"] => string(1) "5"
["jc_name"] => string(12) "客户代表"
["jc_parent_id"] => string(1) "3"
["jc_sort"] => string(1) "2"
["jc_level"] => string(1) "2"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
[4] => array(7) {
["jc_id"] => string(1) "6"
["jc_name"] => string(12) "销售管理"
["jc_parent_id"] => string(1) "2"
["jc_sort"] => string(1) "2"
["jc_level"] => string(1) "1"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
[5] => array(7) {
["jc_id"] => string(1) "7"
["jc_name"] => string(26) "财务/人力资源/行政"
["jc_parent_id"] => string(1) "0"
["jc_sort"] => string(1) "2"
["jc_level"] => string(1) "0"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
[6] => array(7) {
["jc_id"] => string(1) "8"
["jc_name"] => string(6) "财务"
["jc_parent_id"] => string(1) "7"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "1"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
[7] => array(7) {
["jc_id"] => string(1) "9"
["jc_name"] => string(12) "财务专员"
["jc_parent_id"] => string(1) "8"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "2"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
}
}
变成:
array(2) {
[0] => array(8) {
["jc_id"] => string(1) "2"
["jc_name"] => string(20) "销售/客服/市场"
["jc_parent_id"] => string(1) "0"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "0"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(2) {
[0] => array(8) {
["jc_id"] => string(1) "3"
["jc_name"] => string(12) "销售业务"
["jc_parent_id"] => string(1) "2"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "1"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(2) {
[0] => array(8) {
["jc_id"] => string(1) "4"
["jc_name"] => string(12) "销售代表"
["jc_parent_id"] => string(1) "3"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "2"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(0) {
}
}
[1] => array(8) {
["jc_id"] => string(1) "5"
["jc_name"] => string(12) "客户代表"
["jc_parent_id"] => string(1) "3"
["jc_sort"] => string(1) "2"
["jc_level"] => string(1) "2"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(0) {
}
}
}
}
[1] => array(8) {
["jc_id"] => string(1) "6"
["jc_name"] => string(12) "销售管理"
["jc_parent_id"] => string(1) "2"
["jc_sort"] => string(1) "2"
["jc_level"] => string(1) "1"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(0) {
}
}
}
}
[1] => array(8) {
["jc_id"] => string(1) "7"
["jc_name"] => string(26) "财务/人力资源/行政"
["jc_parent_id"] => string(1) "0"
["jc_sort"] => string(1) "2"
["jc_level"] => string(1) "0"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(1) {
[0] => array(8) {
["jc_id"] => string(1) "8"
["jc_name"] => string(6) "财务"
["jc_parent_id"] => string(1) "7"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "1"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(1) {
[0] => array(8) {
["jc_id"] => string(1) "9"
["jc_name"] => string(12) "财务专员"
["jc_parent_id"] => string(1) "8"
["jc_sort"] => string(1) "1"
["jc_level"] => string(1) "2"
["jc_visible"] => string(1) "1"
["is_delete"] => string(1) "0"
["child"] => array(0) {
}
}
}
}
}
}
}
【递归实现无限极分类】递归函数:
public function list_to_tree($list, $child=array(), $pid = 0)
{
$child = array();
if (!empty($list)) {
foreach ($list as $k => &$v) {
if ($v['jc_parent_id'] == $pid) {
$v['child'] = $this->list_to_tree($list, $child, $v['jc_id']);
$child[] = $v;
unset($list_to_tree[$k]);
}
}
}
return $child;
}
}
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM
- pytorch|使用pytorch从头实现多层LSTM