PHPackages                             gilbertchiao/taiwan-county-city - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gilbertchiao/taiwan-county-city

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gilbertchiao/taiwan-county-city
===============================

A PHP library for listing cities and counties in Taiwan, with their English names and other data.

v1.0.0(9mo ago)00MITPHPPHP ^7.4 || ^8.0

Since Jul 19Pushed 9mo agoCompare

[ Source](https://github.com/gilbertchiao/taiwan-county-city)[ Packagist](https://packagist.org/packages/gilbertchiao/taiwan-county-city)[ Docs](https://github.com/gilbertchiao/taiwan-county-city)[ RSS](/packages/gilbertchiao-taiwan-county-city/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

台灣縣市資料 PHP 工具 (Taiwan City/County Data for PHP)
===============================================

[](#台灣縣市資料-php-工具-taiwan-citycounty-data-for-php)

此專案提供一個簡單的方式，在 PHP 應用程式中取用台灣的縣市基本資料。資料來源為交通部運輸資料流通服務平臺 (TDX)，並已預先處理成方便 PHP 直接使用的格式，無需每次都從資料庫查詢或解析 JSON 檔案。

安裝
--

[](#安裝)

您可以使用 Composer 來安裝此套件：

```
composer require gilbertchiao/taiwan-county-city
```

如何使用
----

[](#如何使用)

根據您的 PHP 版本與偏好，選擇以下其中一種方式使用。

### 方法一：使用 `Cities` Class (通用)

[](#方法一使用-cities-class-通用)

這個方法適用於所有 PHP 版本，提供簡單的靜態方法來獲取資料。

**範例:**

```
require_once __DIR__ . '/vendor/autoload.php';

use TaiwanData\Cities;

// 取得所有縣市資料
$allCities = Cities::getAllCities();
print_r($allCities);

// 根據 CountyID 取得新竹市的資料 ('O')
$hsinchuCity = Cities::getCityById('O');
if ($hsinchuCity) {
    echo "新竹市的中文名稱：" . $hsinchuCity['CountyName'] . "\n"; // 輸出: 新竹市
    echo "新竹市的英文名稱：" . $hsinchuCity['County'] . "\n";   // 輸出: Hsinchu
}

// 取得所有縣市的中文名稱列表
$countyNames = Cities::getCountyNames();
print_r($countyNames);

// 根據中文名稱取得臺北市資料
$taipeiCity = Cities::getCityByName('臺北市');
if ($taipeiCity) {
    echo "臺北市的 CountyCode：" . $taipeiCity['CountyCode'] . "\n"; // 輸出: 63000
}
```

### 方法二：使用 `CityEnum` (PHP 8.1+)

[](#方法二使用-cityenum-php-81)

如果您使用 PHP 8.1 或更新版本，建議使用 Enum 以獲得更佳的型別安全與可讀性。

**範例:**

```
require_once __DIR__ . '/vendor/autoload.php';

use TaiwanData\CityEnum;

// 直接使用 Enum Case
$taipei = CityEnum::TAIPEI;

// 獲取詳細資料
$taipeiDetails = $taipei->getDetails();
echo "臺北市的 CountyCode: " . $taipeiDetails['CountyCode'] . "\n"; // 輸出: 63000

// 獲取中文名稱
echo "中文名稱: " . $taipei->getCountyName() . "\n"; // 輸出: 臺北市

// 從 CountyID 動態獲取 Enum Case
$hsinchu = CityEnum::fromCountyId('O');
if ($hsinchu) {
    echo "新竹市的英文名稱: " . $hsinchu->getCountyEnglishName() . "\n"; // 輸出: Hsinchu
}

// 獲取所有縣市的中文名稱
$allNames = CityEnum::getAllCountyNames();
print_r($allNames);
```

資料來源
----

[](#資料來源)

所有資料皆來自[交通部運輸資料流通服務平臺 (TDX)](https://tdx.transportdata.tw) 的「[全臺縣市代碼資料(City) v2](https://tdx.transportdata.tw/api/basic/v2/Basic/City?%24format=JSON)」與「[全臺縣市代碼資料(County) v2](https://tdx.transportdata.tw/api/basic/v2/Basic/County?%24format=JSON)」。

- 原始資料位於 `data/basic_city.json` 與 `data/basic_county.json`。
- 合併後的完整資料為 `data/city_county.json`。

由於此資料不常異動，專案將其轉換為 PHP Class 或 Enum，以便在程式中更有效率地取用。

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance55

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

298d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e021adf12734a685d568f5c76b51d7a59c28168d2ef9a1858f56372b120f55d?d=identicon)[gilbertchiao](/maintainers/gilbertchiao)

---

Top Contributors

[![gilbertchiao](https://avatars.githubusercontent.com/u/222855?v=4)](https://github.com/gilbertchiao "gilbertchiao (1 commits)")

### Embed Badge

![Health badge](/badges/gilbertchiao-taiwan-county-city/health.svg)

```
[![Health](https://phpackages.com/badges/gilbertchiao-taiwan-county-city/health.svg)](https://phpackages.com/packages/gilbertchiao-taiwan-county-city)
```

###  Alternatives

[webignition/absolute-url-deriver

Derives an absolute URL from relative and source URLs

1414.8k10](/packages/webignition-absolute-url-deriver)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
