Last active
September 14, 2016 02:14
-
-
Save fddcddhdd/0f6273dd2b2dc67597b25d15019a53ba to your computer and use it in GitHub Desktop.
郵便番号などのマスタと、手入力された郵便番号のリストを比較して、マスタに存在しない値をリストアップしてくれるWebアプリ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// サンプル初期値を入れておく。POSTされた値があったら、それを使う | |
if(empty($_POST['parents'])){ | |
$posted_parents = "100-0001\n100-0002\n100-0003"; | |
}else{ | |
$posted_parents = $_POST['parents']; | |
} | |
if(empty($_POST['children'])){ | |
$posted_children = "100-0001\n100-0002\n100-0003\n123-4567"; | |
}else{ | |
$posted_children = $_POST['children']; | |
} | |
// 入力フォーム | |
echo <<< EOT | |
<meta charset="UTF-8"> | |
<title>master_check</title> | |
<form method=post> | |
親(マスタ)<br> | |
<textarea name="parents" rows="10" cols="40"> | |
$posted_parents | |
</textarea><br><br> | |
子(参照先)<br> | |
<textarea name="children" rows="10" cols="40"> | |
$posted_children | |
</textarea><br><br> | |
<input type=submit value="比較"> | |
</form> | |
EOT; | |
// POST引数から配列変数に格納 | |
$parents = str2arr($posted_parents); | |
$children = str2arr($posted_children); | |
function str2arr($str){ | |
$arr = explode("\n", $str); // 改行で分割して配列化 | |
$arr = array_map('trim', $arr); // 各行にtrim()をかける | |
$arr = array_filter($arr, 'strlen'); // 文字数が0の行を取り除く | |
return array_values($arr); // これはキーを連番に振りなおしてるだけ | |
} | |
echo "親(マスタ)が存在しない子(参照先)<br><br>\n"; | |
// 比較結果を出力 | |
foreach($children as $child){ | |
$same_flag = false; | |
foreach($parents as $parent){ | |
// 親が存在したら、フラグを立てる | |
if($child == $parent){ | |
$same_flag = true; | |
} | |
} | |
// 親が見つからなかったら表示する | |
if($same_flag == false){ | |
echo $child ."<br>\n"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment