#核心点在于两个临时变量来存储
#$tmp用来存储最大,$history用来存储$tmp被比较下来的值,作为第二大
$array = [1,3,5,18,99,6,109];
$count = count($array);
$tmp = $array[0];
$tmp2 = $array[1];
$history = 0;
for($i=2;$i<$count;$i++){
if($array[$i] > $tmp){
$history = $tmp;
$tmp = $array[$i];
}
if($history > $tmp2){
$tmp2 = $history;
}
}
echo $tmp.'-'.$tmp2;
网友评论