mysql 数据库里规定 city 表 省 市 县,如何使用php 循环三级数组
/* $prvicelist = Db::name("system_city")->field("city_id,parent_id,name as text")->where("parent_id",0)->select()->toArray();
foreach ( $prvicelist as $k=>$v){
$citylist = Db::name("system_city")->field("city_id,parent_id,name as text")->where("parent_id",$v["city_id"])->select()->toArray();
$prvicelist[$k]=$v;
foreach ( $citylist as $kk=>$vv){
$prvicelist[$k]["children"][$kk]=$vv;
$cityxian = Db::name("system_city")->field("city_id,parent_id,name as text")->where("parent_id",$vv["city_id"])->select()->toArray();
foreach ( $cityxian as $kkk=>$vvv){
$prvicelist[$k]["children"][$kk]["children"][$kkk]=$vvv;
}
}
}
循环出的格式[ { text: '浙江', children: [ { text: '杭州', children: [{ text: '西湖区' }, { text: '余杭区' }], }, { text: '温州', children: [{ text: '鹿城区' }, { text: '瓯海区' }], }, ], }, { text: '福建', children: [ { text: '福州', children: [{ text: '鼓楼区' }, { text: '台江区' }], }, { text: '厦门', children: [{ text: '思明区' }, { text: '海沧区' }], }, ], }, ];
方便前端使用