PHP code example of gilbertchiao / taiwan-county-city
1. Go to this page and download the library: Download gilbertchiao/taiwan-county-city library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gilbertchiao / taiwan-county-city example snippets
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
}
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);