PHP、Wordpress用の全国市区町村マスタコード（2022年9月版） 

# 全国市区町村のマスタデータが必要になった
駅データと一緒に全国市区町村データも必要になったので作成。
郵便住所.jpさんからデータを拝借。このサイトは取得データが絞れるので、とてもコードに利用しやすい。
とても汎用性に高い構想で作られていて、住所データ系サイトの中でも素晴らしいサイト。
https://postaladdress.jp/

# マスタコード
県→市町村→区の3レベル構造です。
コメントアウト解除すれば地域→県→市町村→区の4レベル構造にも対応。
https://github.com/obwebsys/obwebsys/blob/master/create-todofuken202209.php

# 生成コード（おまけ）
![スクリーンショット 2023-11-24 121056.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/25349/a2dee67f-0f78-e69e-a3ba-766057c23b93.png)

こちらから上記絞り込みデータを取得し走らせるコードです。

20231127
・$genbun変数に全データ1行区切りでテキストで配置してください。何件かのテスト用で初期化してます。

```php:index.php
<?php
$genbun = '
"03524","二戸郡一戸町","NINOHE GUN ICHINOHE MACHI"
"04101","仙台市青葉区","SENDAI SHI AOBA KU"
"04102","仙台市宮城野区","SENDAI SHI MIYAGINO KU"
"04103","仙台市若林区","SENDAI SHI WAKABAYASHI KU"
"04104","仙台市太白区","SENDAI SHI TAIHAKU KU"
"04105","仙台市泉区","SENDAI SHI IZUMI KU"
"04202","石巻市","ISHINOMAKI SHI"
';
$ary_todofuken = array(
    "01"=>"北海道","02"=>"青森県","03"=>"岩手県","04"=>"宮城県","05"=>"秋田県","06"=>"山形県","07"=>"福島県","08"=>"茨城県","09"=>"栃木県","10"=>"群馬県","11"=>"埼玉県","12"=>"千葉県","13"=>"東京都","14"=>"神奈川県","15"=>"新潟県","16"=>"富山県","17"=>"石川県","18"=>"福井県","19"=>"山梨県","20"=>"長野県","21"=>"岐阜県","22"=>"静岡県","23"=>"愛知県","24"=>"三重県","25"=>"滋賀県","26"=>"京都府","27"=>"大阪府","28"=>"兵庫県","29"=>"奈良県","30"=>"和歌山県","31"=>"鳥取県","32"=>"島根県","33"=>"岡山県","34"=>"広島県","35"=>"山口県","36"=>"徳島県","37"=>"香川県","38"=>"愛媛県","39"=>"高知県","40"=>"福岡県","41"=>"佐賀県","42"=>"長崎県","43"=>"熊本県","44"=>"大分県","45"=>"宮崎県","46"=>"鹿児島県","47"=>"沖縄県");
$ary_todofuken_k = array(
    "01"=>"hokkaido","02"=>"aomori","03"=>"iwate","04"=>"miyagi","05"=>"akita","06"=>"yamagata","07"=>"fukushima","08"=>"ibaraki","09"=>"tochigi","10"=>"gumma","11"=>"saitama","12"=>"chiba","13"=>"tokyo","14"=>"kanagawa","15"=>"niigata","16"=>"toyama","17"=>"ishikawa","18"=>"fukui","19"=>"yamanashi","20"=>"nagano","21"=>"gifu","22"=>"shizuoka","23"=>"aichi","24"=>"mie","25"=>"shiga","26"=>"kyoto","27"=>"osaka","28"=>"hyogo","29"=>"nara","30"=>"wakayama","31"=>"tottori","32"=>"shimane","33"=>"okayama","34"=>"hiroshima","35"=>"yamaguchi","36"=>"tokushima","37"=>"kagawa","38"=>"ehime","39"=>"kochi","40"=>"fukuoka","41"=>"saga","42"=>"nagasaki","43"=>"kumamoto","44"=>"oita","45"=>"miyazaki","46"=>"kagoshima","47"=>"okinawa");
$ary_seireilst = array(
    "011"=>"札幌市","041"=>"仙台市","111"=>"さいたま市","121"=>"千葉市","131"=>"２３区","1410"=>"横浜市","1413"=>"川崎市","1415"=>"相模原市","151"=>"新潟市","2210"=>"静岡市","2213"=>"浜松市","231"=>"名古屋市","261"=>"京都市","2710"=>"大阪市","2714"=>"堺市","281"=>"神戸市","331"=>"岡山市","341"=>"広島市","4010"=>"北九州市","4013"=>"福岡市","431"=>"熊本市"
);
$ary_seireilst_k = array(
    "011"=>"SAPPORO SHI","041"=>"SENDAI SHI","111"=>"SAITAMA SHI","121"=>"CHIBA SHI","131"=>"23-KU","1410"=>"YOKOHAMA SHI","1413"=>"KAWASAKI SHI","1415"=>"SAGAMIHARA SHI","151"=>"NIIGATA SHI","2210"=>"SHIZUOKA SHI","2213"=>"HAMAMATSU SHI","231"=>"NAGOYA SHI","261"=>"KYOTO SHI","2710"=>"OSAKA SHI","2714"=>"SAKAI SHI","281"=>"KOBE SHI","331"=>"OKAYAMA SHI","341"=>"HIROSHIMA SHI","4010"=>"KITAKYUSHU SHI","4013"=>"FUKUOKA SHI","431"=>"KUMAMOTO SHI"
);
$out1 = array();
$tmp_todofukencd = "";
$tmp_areacd = "";
$tmp_areacd2 = "";
$tmp_area_kanji = "";
$tmp_area_kana = "";
$ary_genbun = explode("\n",$genbun);
foreach ($ary_genbun as $ind) {
    if ($ind=="") continue;
    $ary_ind = explode(",",$ind);
    $areacd   = str_replace('"','',$ary_ind[0]);
    $areaKanji = str_replace('"','',$ary_ind[1]);
    $areaRoma = str_replace('"','',$ary_ind[2]);
    // 県を出力
    if ($tmp_todofukencd != substr($areacd,0,2)) {
        $tmp_todofukencd = substr($areacd,0,2);
        $out1[] = '$_area_parent2 = $_func_lvx_create($_area_parent1,$_post_param,"'.$ary_todofuken_k[$tmp_todofukencd].'","'.$ary_todofuken[$tmp_todofukencd].'",'.intval($tmp_todofukencd).',$updateym);';
    }
    // エリアコード3桁目が1の場合は配列の一個前に政令指定都市を追加
    if (substr($areacd,2,1) == 1) {
        $areacd_3k = substr($areacd,0,3);
        $areacd_4k = substr($areacd,0,4);
        if ($tmp_areacd != $areacd_3k && $tmp_areacd != $areacd_4k) {
            // 3,4ケタが一致したら政令指定都市を追加
            if (array_key_exists($areacd_3k,$ary_seireilst)) {
                $_Kanji = $ary_seireilst[$areacd_3k];
                $_Roma = $ary_seireilst_k[$areacd_3k];
                $tmp_areacd = $areacd_3k;
            }
            if (array_key_exists($areacd_4k,$ary_seireilst)) {
                $_Kanji = $ary_seireilst[$areacd_4k];
                $_Roma = $ary_seireilst_k[$areacd_4k];
                $tmp_areacd = $areacd_4k;
            }
            $_Roma = str_replace(' SHI','-SHI',$_Roma);
            $_Roma = strtolower($_Roma);
            $_areacd = substr($areacd,0,4)."0";
            if ($tmp_areacd2 != $_areacd && $tmp_area_kanji != $_Kanji) {
                $out1[] = '$_area_parent3 = $_func_lvx_create($_area_parent2,$_post_param,"'.$_Roma.'","'.$_Kanji.'",'.intval($_areacd).',$updateym);';
                $tmp_areacd2 = $_areacd;
                $tmp_area_kanji = $_Kanji;
                $tmp_area_kana = $_Roma;
            }
        }
        // 区を追加
        $areaRoma = str_replace(' KU','-KU',$areaRoma);
        $areaRoma = strtolower($areaRoma);
        $areaRoma = str_replace(' ','-',$areaRoma);
        $areaRoma = str_replace($tmp_area_kana."-",'',$areaRoma);
        $areaKanji = str_replace($tmp_area_kanji,'',$areaKanji);
        $out1[] = '$_area_parent4 = $_func_lvx_create($_area_parent3,$_post_param,"'.$areaRoma.'","'.$areaKanji.'",'.intval($areacd).',$updateym);';
    } else {
        // 市町村を追加
        $areaRoma = str_replace(' SHI','-SHI',$areaRoma);
        $areaRoma = str_replace(' CHO','-CHO',$areaRoma);
        $areaRoma = str_replace(' MACHI','-MACHI',$areaRoma);
        $areaRoma = str_replace(' MURA','-MURA',$areaRoma);
        $areaRoma = str_replace(' SON','-SON',$areaRoma);
        $areaRoma = strtolower($areaRoma);
        $areaRoma = str_replace(' ','-',$areaRoma);
        // ローマ字に郡が含まれていれば冗長なのでそれ以前を削除
        $areaRoma = str_replace(strstr($areaRoma,'-gun-',true).'-gun-','',$areaRoma);
        $out1[] = '$_area_parent3 = $_func_lvx_create($_area_parent2,$_post_param,"'.$areaRoma.'","'.$areaKanji.'",'.intval($areacd).',$updateym);';
    }
}
// 整形
echo('<pre>'); foreach ($out1 as $ind) { echo $ind."<br>"; } echo('</pre>');
?>
```