php 地区三级联动

Posted GavinXxg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 地区三级联动相关的知识,希望对你有一定的参考价值。

/**
     * 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 地区三级联动的主要内容,如果未能解决你的问题,请参考以下文章

AJAX+PHP实现三级联动

帝国CMS7.5实现地区三级联动并且前台可地区筛选的教程

跪求带MYSQL数据库的中国地区的三级联动下拉

省份地区三级联动的简单实现

织梦联动类型地区联动三级修复以及省份-市级-地区分开+高亮

省份,城市,地区------三级联动菜单