其中 $fang $chang $color都是有N种情况,如果按照我以下的这种写法,我想到了死……
但是万能的网友一定能够帮我找一个简便的方法。
if ($fang == 1 and $chang == 1 and $color == 1){
$mm = "margin-left: 10px;";
$kuan = "450px;";
}elseif ($fang == 2 and $chang == 1 and $color == 1) {
$mm = "margin-left: 6px;";
$kuan = "420px;";
}elseif ($fang == 3 and $chang == 1 and $color == 1) {
$mm = "margin-left: 5px;";
$kuan = "410px;";
}elseif ($fang == 4 and $chang == 1 and $color == 1) {
$mm = "margin-left: 10px;";
$kuan = "400px;";
}elseif ($fang == 1 and $chang == 2 and $color == 1) {
$mm = "margin-left: 15px;";
$kuan = "470px;";
}
………………
你得去发现margin-left、宽的值和$fang ,$chang,$color的关系,有木有规律,例如根据$fang和$chang的变量去计算margin-left多少,宽多少。
如果没规律,很抱歉,貌似你还真的一直if else下去。
也没感觉下面的写法比if else好多少,你觉得呢
以下代码只是想当然写的,没运行过,思路你应该看得懂,祝好运!
$a = array(
//第一层是fang的值
'1'=>array(
//第二层是chang的值
'1'=>array(
//第三层是color的参,不同的情况下,自行修改mm和kuan的值
'1'=>array("mm"=>"margin-left:10px;", "kuan"=>"100px"),
'2'=>array("mm"=>"margin-left:10px;", "kuan"=>"200px"),
'3'=>array("mm"=>"margin-left:10px;", "kuan"=>"300px")
),
'2'=>array(
'1'=>array("mm"=>"margin-left:10px;", "kuan"=>"400px"),
'2'=>array("mm"=>"margin-left:10px;", "kuan"=>"500px"),
'3'=>array("mm"=>"margin-left:10px;", "kuan"=>"600px")
),
'3'=>array(
'1'=>array("mm"=>"margin-left:10px;", "kuan"=>"700px"),
'2'=>array("mm"=>"margin-left:10px;", "kuan"=>"800px"),
'3'=>array("mm"=>"margin-left:10px;", "kuan"=>"900px")
)
),
'2'=>array(
'1'=>array("mm"=>"margin-left:10px;", "kuan"=>"450px")
),
'3'=>array(
'1'=>array("mm"=>"margin-left:10px;", "kuan"=>"450px")
),
'4'=>array(
'1'=>array("mm"=>"margin-left:10px;", "kuan"=>"450px")
)
);
//取用,例如,fang chang color的值为 1 2 1,则输出mm和kuan
$mm = $a['1']['2']['1']['mm'];
$kuan = $a['1']['2']['1']['kuan'];
echo $kuan;
如果是这种简单的==比较, 也可以采用所谓的xx模式, 即, 建个数组:
$maps = array();$maps['1x1x1'] = array('mm' => '...', 'kuan' => '..');
//...
或多维数组. 用的时候, 就直接索引取
print $maps[$fang . 'x' . $chang . 'x' . $color]['mm'];