Home >>PHP Programs >Find Minimum value of an array

Find Minimum value of an array

How to find minimum value of an array.

Eg
<?php
$arr=array(100,11,12,0,13,10,15);
$temp=$arr[0];
foreach($arr as $x)
{
	if($x<$temp)
	{
		$temp=$x;
	}
}
echo "Minimum value of array = ".$temp;
?>

Explanation

First create an array with some numeric value. Now create a temporary variable $temp in which stored array's first index's value i.e 100. Now to display/fetch array's all value from start to end used foreach loop. Inside foreach create a temporary variable $x which holds array values. Now add a condition in body of foreach to check $x is less than $temp then assign $x value to $temp variable this process will contiue till end of array. after completion of foreach loop $temp variable has minimum value from array $arr. Now print mininum value.

No Sidebar ads