hrvthzslt/php-country-list

列出国家的 alpha2, alpha3, 数字和isd代码

v1.0.0 2019-04-23 14:38 UTC

This package is not auto-updated.

Last update: 2024-09-19 16:15:17 UTC


README

列出国家 alpha2, alpha3, 数字和isd代码以及洲名和代码

用法

列表

收集所有国家

$phpCountryList = new \HrvthZslt\PhpCountryList\PhpCountryList();
$countries = $phpCountryList->getAllCountries();

$countries 内容

  ...
  156 => HrvthZslt\PhpCountryList\Models\Country {#1340
    +name: "Netherlands"
    +alpha2: "NL"
    +alpha3: "NLD"
    +numeric: "528"
    +isd: 31
    +continent: HrvthZslt\PhpCountryList\Models\Continent {#1341
      +name: "Europe"
      +code: "EU"
    }
  }
  157 => HrvthZslt\PhpCountryList\Models\Country {#1342
    +name: "New Caledonia"
    +alpha2: "NC"
    +alpha3: "NCL"
    +numeric: "540"
    +isd: 687
    +continent: HrvthZslt\PhpCountryList\Models\Continent {#1343
      +name: "Oceania"
      +code: "OC"
    }
  }
  ...

收集某个洲的国家

$phpCountryList = new \HrvthZslt\PhpCountryList\PhpCountryList();
$countries = $phpCountryList->getAllCountriesForContinent('NA');

搜索

通过 alpha2, alpha3, 数字代码或isd代码搜索国家

$country = $phpCountryList->getCountryByAlpha2('HM');
$country = $phpCountryList->getCountryByAlpha3('HMD');
$country = $phpCountryList->getCountryByNumericCode('334');
$country = $phpCountryList->getCountryByIsd(672);

这些搜索结果为

HrvthZslt\PhpCountryList\Models\Country {#906
  +name: "Heard Island and Mcdonald Islands"
  +alpha2: "HM"
  +alpha3: "HMD"
  +numeric: "334"
  +isd: 672
  +continent: HrvthZslt\PhpCountryList\Models\Continent {#907
    +name: "Antarctica"
    +code: "AN"
  }
}

国家实例

检查国家所属洲,返回true或false

/** @var HrvthZslt\PhpCountryList\Models\Country $country */
$result = $country->inContinent('EU');

转换为数组

/** @var HrvthZslt\PhpCountryList\Models\Country $country */
$contryArray = $country->toArray();

$countryArray 值

array:6 [
  "name" => "Antarctica"
  "alpha2" => "AQ"
  "alpha3" => "ATA"
  "numeric" => "010"
  "isd" => 672
  "continent" => array:2 [
    "name" => "Antarctica"
    "code" => "AN"
  ]
]