/** * 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
数据返回结果: