How to find minimum value of an array with example

How to find minimum value of an array with example - Image

It's very simple to find the minimum value of an array using foreach loop. Create an array with some numeric value.

Now create a temporary variable $temp in which stored array’s first index’s value i.e 45.

$arr=array(45,78,485,58,5,45,47);
$temp=$arr[0];
foreach($arr as $x)
{
	if($x<$temp)
	{
		$temp=$x;
	}
}
echo "Minimum value of array = ".$temp;

Output:

Minimum value of array = 5

Related posts

Write a comment