PHP 楼栋,单元,楼层,房屋数据的处理

        //查询所有房屋数据
        $sql   = "select build,unit,floor_num,room from room where region = 瑞丰锦园小区 order by build,unit,floor_num,room";
        $house = pdo_fetchall($sql);


        #利用数组key相同放一起的特性,将同一个楼栋-单元-楼层的数据放一起
        $tmpF = [];
        foreach($house as $k => $v){
            $tmpF[$v['build'].'-'.$v['unit'].'-'.$v['floor_num']][] = $v;
        }
        $tmpFk = [];
        $num   = 0;
        //将有用的数据提取出来
        foreach ($tmpF as $kF => $vF){
            $_tmp = explode("-", $kF);

            $tmpFk[$num]['build'] = $_tmp[0];
            $tmpFk[$num]['unit']  = $_tmp[1];
            $tmpFk[$num]['floor_num']  = $_tmp[2];
            $tmpFk[$num]['floorArr'] = $vF;
            $num = $num +1;
        }

        $tmpUn = [];
        foreach($tmpFk as $kU => $vU){
            $tmpUn[$vU['build'].'-'.$vU['unit']][] = $vU;
        }

        $tmpUk = [];
        $numUk  = 0;
        foreach ($tmpUn as $kUn => $vUn){
            $__tmp = explode("-", $kUn);

            $tmpUk[$numUk]['build'] = $__tmp[0];
            $tmpUk[$numUk]['unit']  = $__tmp[1];
            $tmpUk[$numUk]['unitArr'] = $vUn;
            $numUk = $numUk +1;
        }

        $tmpB = [];
        foreach($tmpUk as $kUk => $vUk){
            $tmpB[$vUk['build']][] = $vUk;
        }

        $tmpBk = [];
        $numBk  = 0;
        foreach ($tmpB as $kB => $vB){
            $tmpBk[$numBk]['build'] = $kB;
            $tmpBk[$numBk]['buildArr'] = $vB;
            $numBk = $numBk +1;
        }

        echo json_encode($tmpBk);
        exit();
    

 现在有楼栋-单元-楼层-房间号 的数据,需要按照层级关系处理。

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页