LoginSignup
1
1

More than 5 years have passed since last update.

都道府県名、区市町村名から市町村コードを吐き出す関数を考えてみたものの

Posted at

市町村コードの対応データをjsonで書きだしてみる - Qiitaで全国地方公共団体コードjsonができたので、都道府県名、区市町村名から市町村コードを吐き出す関数を考えてみた。

address2citycade.php
<?php
function address2citycade($prefname, $cityname)
{
$req =  'citycode.json';
$json = file_get_contents($req);
$json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$addcode = json_decode($json, true);
$prefid = array_search($prefname, array_column($addcode,'pref-name', 'pref-code'));
$sitycode = strval(array_search($cityname, array_column($addcode[$prefid]['data'],'name', 'id')));
return $sitycode;
}

echo address2citycade("東京都","新宿区");

こんな感じで良いかな、と思っていたら、同一都道府県に同一名称の区があるところがあるんですね。
同一都道府県内の同一名称の市区町村(市区町村雑学)
神奈川県の横浜市南区と相模原市南区
同じく横浜市緑区と相模原市緑区
大阪府の大阪市西区と堺市西区
大阪市北区と堺市北区

以上4組

同一都道府県の同一市町村名に関しては、以前はあったらしいけれど、現在はないらしいので良さそう。

それにしても、さてどうしましょ?

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1