PHP递归

function recursion($data, $id = 0, $level = 0)
    {
        $list = array();
        foreach ($data as $v) {
            if ($v['pid'] == $id) {
                $v['level'] = $level;
                $v['son'] = $this->recursion($data, $v['id'], $level + 1);
                if (empty($v['son'])) {
                    unset($v['son']);
                }
                array_push($list, $v);
            }
        }
        return $list;
    }
function generateTree($data){
        $items = array();
        foreach($data as $v){
            $items[$v['id']] = $v;
        }
        $tree = array();
        foreach($items as $k => $item){
            if(isset($items[$item['pid']])){
                $items[$item['pid']]['son'][] = &$items[$k];
            }else{
                $tree[] = &$items[$k];
            }
        }
        return $tree;
    }
上一篇:[luogu7600]封闭道路


下一篇:【题解】Count on a tree