大卡实验室 / 行政区划代码
中国行政区划代码
v2.3.8
2021-01-05 08:45 UTC
Requires
- php: >=7
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2
- phpunit/phpunit: ^7
- symfony/css-selector: ^4.3
- symfony/dom-crawler: ^4.3
README
中国行政区划代码 (http://www.mca.gov.cn/article/sj/xzqh/)
此库有两种存储方式:PHP数组文件和SQLite3数据库,请参考以下基准测试。库将自动检测您的PHP是否支持SQLite3,如果支持,则使用SQLite3,否则将回退到使用PHP数组。
您也可以使用函数 useSQLite($v = true)
来开启或关闭使用SQLite。
安装
composer require dakalab/division-code
用法
use Dakalab\DivisionCode\DivisionCode;
$divisionCode = new DivisionCode;
$res = $divisionCode->get('110000'); // 北京市
// Get all the provinces
$provinces = $divisionCode->getAllProvinces();
print_r($provinces);
// Get all the cities in the specified province
$province = '110000';
$cities = $divisionCode->getCitiesInProvince($province);
print_r($cities);
// Get all the counties in the specified city
$city = '445200';
$counties = $divisionCode->getCountiesInCity($city);
print_r($counties);
// Get all the cities in the specified province by the province name
$province = '广东省';
$cities = $divisionCode->getCitiesByProvinceName($province);
print_r($cities);
升级
如果您想自行升级行政区划代码,可以简单地运行 Upgrader
use Dakalab\DivisionCode\Upgrader;
$upgrader = new Upgrader;
$upgrader->upgrade();
基准测试
在MacBook Pro 2.9 GHz Intel Core i5、8GB 1867 MHz DDR3上运行了1000000次循环调用
使用sqlite3
耗时:23.28秒,内存消耗:15.85 KB
使用PHP数组
耗时:0.47秒,内存消耗:287.45 KB
结论
SQLite3内存使用较少但速度较慢,内置的PHP数组速度更快但内存消耗更多。