月度归档:2018年07月

php递归遍历无限分类组织架构

/**
     * @param $data [['value'=>1,'pid'=>0,'label'=>'全国'],['value'=>2,'pid'=>1,'label'=>'xx销售部']]
     * @param $pid
     * @return array|string [['value'=>1,'label'=>'全国','children'=>['value'=>2,'label'=>'xx销售部']]
     */
    public static function getTree($data, $pid)
    {
        $tree = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['children'] = static::getTree($data, $v['value']);
                if (empty($v['children'])) unset($v['children']);//删除空的后代节点
                unset($v['pid']);//删除无需展示的字段
                $tree[] = $v;
            }

        }

        return $tree;
    }
0