Find the Largest Number in an Array using php with example

Find the Largest Number in an Array using php with example

Find the Largest Number in an Array using php with example. Example  for getting largest number in an array:

$array = array(20,18,95,45,48);
 
$max1 = 0;
$max2 = 0;
 
for($i=0; $i<count($array); $i++)
{
    if($array[$i] > $max1)
    {
      $max2 = $max1;
      $max1 = $array[$i];
    }
    else if($array[$i] > $max2)
    {
      $max2 = $array[$i];
    }
}
echo "Largest  value = ".$max1;
echo "<br />"; 
echo "Second Largest Value = ".$max2;

Output:

Maximum value = 95
Second maximum Value = 48

Related posts

Write a comment