Find the second Highest value in array.



$array = array('200', '15','69','122','50','201');
$max_1 = $max_2 = 0;

for($i=0; $i{
    if($array[$i] > $max_1)
    {
      $max_2 = $max_1;
      $max_1 = $array[$i];
    }
    else if($array[$i] > $max_2)
    {
      $max_2 = $array[$i];
    }
}
echo "Max=".$max_1;
echo "
";
echo "Smax 2=".$max_2;

?>

Reference :

max — Find highest value

echo max(2, 3, 1, 6, 7);  // 7
echo max(array(2, 4, 5)); // 5

min() - Find lowest value

count() - Count all elements in an array, or something in an object

No comments:

Post a Comment