1.分类(当分类只有2级的时候这种写好会好点)

$data = [
            ["id" => 1, "name" => "shanghai", "pid" => 0],
            ["id" => 2, "name" => "beijing", "pid" => 0],
            ["id" => 3, "name" => "jiangsu", "pid" => 0],
            ["id" => 4, "name" => "huangpu", "pid" => 1],
            ["id" => 5, "name" => "dongchengqu", "pid" => 2],
            ["id" => 6, "name" => "suzhou", "pid" => 3],
        ];
        $result = [];
        foreach ($data as $value) {
            $pid = $value['pid'];
            if ($pid > 0) {
                $result[$pid]['children'] = $value;
            } else {
                $value['children'] = [];
                $result[$value['id']] = $value;
            }
        }

兰亭
3 声望0 粉丝

认识的人越多,我就越喜欢狗