Home >>PHP Programs >PHP get max value in array

PHP get max value in array

How to get maximum value from array in PHP using Library function, foeach Loop, for loop

PHP get max value in array using Library function

<?php 

//using library function
$arr= array(110, 20, 52 ,105, 56, 89, 96);
echo max($arr);

?>

PHP get max value in array using foreach

<?php 

$arr= array(110, 20, 52 ,105, 56, 89, 96);
//using foreach loop
$b = 0;
foreach ($arr as $key=>$val)
 {
    if ($val > $b) {
        $b = $val;
    }
}
echo $b;
?>

PHP get max value in array using for loop

<?php 

$arr= array(110, 20, 52 ,105, 56, 89, 96);
//using forloop 
$b = 0;
for($i=0;$i<count($arr);$i++)
 {
    if ($arr[$i] > $b)
	{
        $b = $arr[$i];
    }
}
echo $b;
?>

No Sidebar ads