递归实现无限极分类

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;
    }
}

你可能感兴趣的:(递归实现无限极分类)