Get Minimum Value From Array PHP

PHP get or find min or minimum or lowest value from array; Through this tutorial, i am going to show you how to find or get the min or minimum or lowest value from an array in PHP.

PHP Get the Min or Minimum value in an array

Using the PHP min() function, you can find or get minimum or lowest value from an array in PHP.

Now, i will take some examples using php min() function, for loop and without min() function to get or find min or minimum value from array in PHP.

  • Example – 1 Get min value in array PHP using min() function
  • Example 2 – Find min value from an array without using PHP array functions
  • Example – 3 PHP get min value in array using for loop

Example – 1 Get min value in array PHP using min() function

Let’s see the following example to get or find the smallest or minimum value in the array in php; as follows:

<?php
$array = [1, 10, 50, 40, 2, 15, 100];
// get lowest or minimum value in array php
$res = min($array);
print_r($res);
?> 
The output of the above php code is: 1

Example 2 – Find min value from an array without using PHP array functions

Let’s see the second example for find or get the min or minimum number in array PHP without function; as follows:

<?php
$array = [1000,400,10,50,170,500,45];
$min = $array[0];
foreach($array as $key => $val){
    if($min > $val){
        $min = $val;
        
    }
}	
// get lowest or minimum value in array php using foreach
print $min;
?>
 The output of the above program is: 10

Example – 3 PHP get min value in array using for loop

Let’s take third example for find the minimum or min or smallest value in array PHP without using any function; as follows:

<?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;
?>
  The output of the above program is: 4

Recommended PHP Tutorials

Leave a Comment