※ ChatGPTを利用し、要約された質問です(原文:IF文の記述をシンプルにする方法)
IF文の記述をシンプルにする方法
このQ&Aのポイント
IF文を簡潔に記述する方法について教えてください。
IF文のネストを減らしてシンプルに書く方法を教えてください。
27通りの処理を行うIF文をより簡単な方法で書く方法を教えてください。
いつもお世話になります。
下記のようにIF入れ子で分岐をして3×3×3で27通りの処理をさせたいのですが、もっとシンプルに記述方法があれば御指導お願い致します。
if($asa_ch==1 and $asa_ch_ok ==1){
if($hiru_ch==1 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 1;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 2;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 3;
}
}elseif($hiru_ch==2 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 4;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 5;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 6;
}
}elseif($hiru_ch==3 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 7;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 8;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 9;
}
}elseif($asa_ch==2 and $asa_ch_ok ==1){
if($hiru_ch==1 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 10;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 11;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 12;
}
}elseif($hiru_ch==2 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 13;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 14;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 15;
}
}elseif($hiru_ch==3 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 16;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 17;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 18;
}
}elseif($asa_ch==3 and $asa_ch_ok ==1){
if($hiru_ch==1 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 19;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 20;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 21;
}
}elseif($hiru_ch==2 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 22;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 23;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 24;
}
}elseif($hiru_ch==3 and $hiru_ch_ok == 1){
if($yoru_ch ==1 and $hiru_ch_ok ==1){
$flag = 25;
}elseif($yoru_ch ==2 and $hiru_ch_ok ==1){
$flag = 26;
}elseif($yoru_ch ==3 and $hiru_ch_ok ==1){
$flag = 27;
}
}
}
お礼
edomin2004様 ありがとうございます。 動作を確認し改めて感動しました。 ダラダラと長文になって困っていましたが、 本当にこんなに短くなってすっきりできて感動です。 ありがとうございました。