php 地区三级联动
2021-02-03 23:14
标签:数据库表 mamicode val color mic each sub 数据库 reac 数据库表: 链接:https://pan.baidu.com/s/1L29fBe2EHblJbmBuzf38Ng 提取码:6ch0 数据返回结果: php 地区三级联动 标签:数据库表 mamicode val color mic each sub 数据库 reac 原文地址:https://www.cnblogs.com/xiangangXu1997/p/13153804.html/**
* 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;
}
下一篇:js 实现放大镜效果