Skip to content

Instantly share code, notes, and snippets.

@bookiu
Last active January 21, 2021 13:04
Show Gist options
  • Save bookiu/43873178ab6b6347b0466d0c0a262780 to your computer and use it in GitHub Desktop.
Save bookiu/43873178ab6b6347b0466d0c0a262780 to your computer and use it in GitHub Desktop.
多级省市区嵌套实现
package main
func main() {
}
<?php
$data = file_get_contents("http://preview.www.mca.gov.cn/article/sj/xzqh/2020/2020/202101041104.html");
preg_match_all('#<td class=xl\d+?>(<span.*?)?(\d+|[\x{4e00}-\x{9fa5}]+).*?</td>#u', $data, $matches);
$administratives = [];
$node = array(
'id' => '',
'name' => ''
);
$fixNode = array(
'id' => '',
'name' => '市辖区'
);
foreach ($matches[2] as $idx => $v) {
if ($idx % 2 == 0) {
$node = array(
'id' => $v,
'name' => ''
);
if (in_array($v, ['110000', '120000', '310000', '500000'])) {
$v[3] = '1';
$fixNode['id'] = $v;
}
} else if ($idx % 2 == 1) {
$node['name'] = $v;
$administratives[] = $node;
if (in_array($v, ['北京市', '天津市', '上海市', '重庆市'])) {
$administratives[] = $fixNode;
}
}
}
unset($node);
$root = array();
$nodeMap = array();
foreach ($administratives as $item) {
$rawId = (string)$item['id'];
$id = (int)$rawId;
$provinceId = (int)($id / 10000);
$cityId = ((int)($id / 100)) % 100;
$districtId = $id % 100;
if ($cityId === 0 && $districtId === 0) {
// 省份信息
$node = array(
'Id' => $rawId,
'Name' => $item['name'],
'Cities' => []
);
$nodeMap[$id] = &$node;
$root[] = &$node;
} else if ($districtId === 0) {
// 市信息
$fullCityId = $provinceId * 10000 + $cityId * 100;
$node = array(
'Id' => $rawId,
'Name' => $item['name'],
'Districts' => []
);
$nodeMap[$fullCityId] = &$node;
$provinceNode = &$nodeMap[$provinceId * 10000];
$provinceNode['Cities'][] = &$node;
} else {
// 区信息
$fullCityId = $provinceId * 10000 + $cityId * 100;
$node = array(
'Id' => $rawId,
'Name' => $item['name'],
);
$cityNode = &$nodeMap[$fullCityId];
$cityNode['Districts'][] = $node;
}
unset($node);
}
print_r($root);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment