时间:2021-07-01 10:21:17 帮助过:24人阅读
$a = array('50'=>'5','10'=>'1');
$s = 51;
function test($s,$a){
$flag = 0;
foreach($a as $k=>$v){
if($s >=$k){
$flag=$v;
}
}
return $flag;
}
echo test($s,$a);
$a = array('50'=>'5','10'=>'1');
$s = 51;
function test($s,$a){
$flag = 0;
foreach($a as $k=>$v){
if($s >=$k){
$flag=$v;
break; //这样比较规范(一个模块只有一个出口)
}
}
return $flag;
}
echo test($s,$a);
[code=php]$a = array('50'=>'5','10'=>'1');
$s = 51;
function test($s,$a){
$flag = 0;
foreach($a as $k=>$v){
if($s >=$k){
if($flag<$v)
$flag=$v;
}
}
return $flag;
}
echo test($s,$a);