In this blog, we will be going to find the minimum value in an array using PHP. This will help you find out many ways to find minimum value in an array with several built-in functions.
Example 1: Finding minimum value using min() function
<?php
$array = [1, 10, 50, 40, 2, 15, 100];
// get lowest or minimum value in array php
$res = min($array);
print_r($res);
// 1
?>
Example 2: Finding minimum value using for loop
<?php
$array = [1000,500,10,56,560,6,4];
$min = $array[0];
// get lowest or minimum value in array php using for loop
foreach($array as $key => $val){
if($min > $val){
$min = $val;
}
}
print $min;
//4
?>
Example 3: Finding minimum value using loop
<?php
$numbers = array(5, 3, 8, 2, 9, 1);
$min = $numbers[0];
for ($i = 1; $i < count($numbers); $i++) {
if ($numbers[$i] < $min) {
$min = $numbers[$i];
}
}
echo $min;
//1
?>
Example 4: Finding minimum value using sort() function
<?php
$numbers = array(5, 3, 8, 2, 9, 1);
sort($numbers);
$min = $numbers[0];
echo $min;
//1
?>
I hope this will help you in the programming of PHP. Let me know in the comments if you have any doubt.
0 Comments