码迷,mamicode.com
首页 > Web开发 > 详细

php 地区三级联动

时间:2020-06-17 18:26:50      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:数据库表   mamicode   val   color   mic   each   sub   数据库   reac   

/**
     * Notes:省市级联动
     * Created by xxg@seabig.cn
     * Date: 2020/6/4
     * Time: 17:24
     */
    public function getCityData()
    {
        $cityList = $this->getAll(array("pid = 1"));
        $data = array();
        foreach ($cityList as $v) {
            $data[$v[‘id‘]] = array(
                "id" => $v[‘id‘],
                "name" => $v[‘name‘],
                "subList" => array()
            );
            //获取二级
            $cityTow = $this->getAll(array("pid = {$v[‘id‘]}"));
            foreach ($cityTow as $l) {
                $data[$v[‘id‘]][‘subList‘][$l[‘id‘]] = array(
                    "id" => $l[‘id‘],
                    "name" => $l[‘name‘],
                    "subList" => array()
                );
                //获取三级
                $cityThree = $this->getAll(array("pid = {$l[‘id‘]}"));
                foreach ($cityThree as $t) {
                    $data[$v[‘id‘]][‘subList‘][$l[‘id‘]][‘subList‘][] = array(
                        "id" => $t[‘id‘],
                        "name" => $t[‘name‘]
                    );
                }
            }
        }
        //格式化数据
        $res = array();
        foreach ($data as $val) {
            $item = array();
            foreach ($val[‘subList‘] as $v) {
                $item[] = $v;
            }
            $res[] = array(
                ‘id‘ => $val[‘id‘],
                ‘name‘ => $val[‘name‘],
                ‘subList‘ => $item
            );
        }
        return $res;
    }

数据库表:

链接:https://pan.baidu.com/s/1L29fBe2EHblJbmBuzf38Ng 

提取码:6ch0

数据返回结果: 

技术图片

 

 

技术图片

 

php 地区三级联动

标签:数据库表   mamicode   val   color   mic   each   sub   数据库   reac   

原文地址:https://www.cnblogs.com/xiangangXu1997/p/13153804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!