function dut($f,$dut,$s,$e)
{
$mp = getMax($f[$dut]['binpercent'],$s-1,$e-1);
$resultsArray['Dut'] = 'Bin';
//echo "the max percent is : ".$mp."(bin%".(array_search($mp,$f[$dut]['binpercent'])+1).")<br/>";
//echo $dut."=> bin".(array_search($mp,$f[$dut]['binpercent'])+1)."<br/>";
//echo "=================<br/>";
if(floatval($mp) != 0 )
return array($dut,array_search($mp,$f[$dut]['binpercent'])+1);
else
{
return array($dut,"-");
}}其中$s是起始,$e是终止,程序意思就是说在$s,$e这个范围内取最大值。
现在假设范围是1-8,另知其中一些项要排除,以该形式出现 “string = 1,3,4 ”
现在求剩下的2,5,6,7,8中的最大值,这样的话,程序如何改?
function dut($s, $e, $exString){
$exclude = explode(",", $exString);
$arr = Array();
for($i = $s; $i <= $e; $i++){
if(!in_array($i, $exclude)){
$arr[] = $i;
}
}
$maxValue = max($arr);
return $maxValue;
}
?>
1|110|0|0|0|0|0|0|0|110|100%|0.00%|0.00%|0.00%|0.00%|0.00%|0.00%|0.00%,
2|110|0|0|0|0|0|0|0|110|100%|0.00%|0.00%|0.00%|0.00%|0.00%|0.00%|0.00%,
....
对应名称
dut bin1 bin2 bin3 ... bin8 bin_all bin1% bin2% ... bin8%
其中bin_all是bin1+bin2+...+bin8的和再贴一个getMax的结果:
the max percent is : 12.14%(bin%5)
8=> bin5
=================
the max percent is : 99.43%(bin%6)
21=> bin6
=================
the max percent is : 31.03%(bin%5)
31=> bin5
=================
the max percent is : 11.49%(bin%7)
37=> bin7
=================
the max percent is : 40.23%(bin%6)
41=> bin6
=================
the max percent is : 14.37%(bin%6)
51=> bin6
=================
the max percent is : 3.53%(bin%5)
81=> bin5
=================
the max percent is : 11.18%(bin%7)
93=> bin7
=================
the max percent is : 80.59%(bin%7)
94=> bin7
=================
the max percent is : 8.24%(bin%5)
96=> bin5
=================
the max percent is : 10%(bin%7)
102=> bin7
=================
function getMax($array,$s,$e='')
{
$f = @array_splice($array,$s,$e);
$i = 0;
foreach($f as $v){
if(rtrim($v,'%')+0 > rtrim($i,'%')+0){
$i = $v;
}
}
return $i;
}
function getMax($array,$s,$e, $Exclude='') {
$f = @array_splice($array,$s,$e);
if(! empty($Exclude)) {
foreach(split(',', $Exclude) as $k)
unset($f[$k-1]);
}
$i = 0;
foreach($f as $v){
if(rtrim($v,'%')+0 > rtrim($i,'%')+0){
$i = $v;
}
}
return $i;
}
调用形式
$mp = getMax($f[$dut]['binpercent'],$s-1,$e-1, '1,3,4'); 若串 'string = 1,3,4 ' 也需要他处理,则将
if(! empty($Exclude)) {
foreach(split(',', $Exclude) as $k)
unset($f[$k-1]);
}
改为
if(! empty($Exclude)) {
preg_match_all('/\d+/', $Exclude, $r);
foreach($r[0] as $k)
unset($f[$k-1]);
}调用形式
$mp = getMax($f[$dut]['binpercent'],$s-1,$e-1, 'string = 1,3,4 ');