项目作者: dakalab

项目描述 :
Administrative division codes of China (http://www.mca.gov.cn/article/sj/xzqh/)
高级语言: PHP
项目地址: git://github.com/dakalab/division-code.git
创建时间: 2018-11-21T10:38:31Z
项目社区:https://github.com/dakalab/division-code

开源协议:MIT License

下载


division code

Build Status
Codecov
Latest Stable Version
Total Downloads
License

Administrative division codes of China (http://www.mca.gov.cn/article/sj/xzqh/)

This library has two ways of storage: php array file and SQLite3 database, see the benchmark below. The library will automatically detect if your php support SQLite3, if yes then it will use SQLite3, otherwise it will fall back to use php array.

You can also use function useSQLite($v = true) to turn on or turn off using SQLite.

Install

  1. composer require dakalab/division-code

Usage

  1. use Dakalab\DivisionCode\DivisionCode;
  2. $divisionCode = new DivisionCode;
  3. $res = $divisionCode->get('110000'); // 北京市
  4. // Get all the provinces
  5. $provinces = $divisionCode->getAllProvinces();
  6. print_r($provinces);
  7. // Get all the cities in the specified province
  8. $province = '110000';
  9. $cities = $divisionCode->getCitiesInProvince($province);
  10. print_r($cities);
  11. // Get all the counties in the specified city
  12. $city = '445200';
  13. $counties = $divisionCode->getCountiesInCity($city);
  14. print_r($counties);
  15. // Get all the cities in the specified province by the province name
  16. $province = '广东省';
  17. $cities = $divisionCode->getCitiesByProvinceName($province);
  18. print_r($cities);

Upgrade

If you want to upgrade the division codes by yourself, you can simply run the Upgrader

  1. use Dakalab\DivisionCode\Upgrader;
  2. $upgrader = new Upgrader;
  3. $upgrader->upgrade();

Benchmark

For a loop of 1000000 calls ran on a MacBook Pro 2.9 GHz Intel Core i5, 8GB 1867 MHz DDR3:

Use sqlite3

Time cost: 23.28 s, Memory cost: 15.85 kb

Use php array

Time cost: 0.47 s, Memory cost: 287.45 kb

Conclusion

SQLite3 uses less memory usage but slower, built-in php array is much faster but costs much more memory.