PHP Get Min or Minimum Value in Array
Php 12-Aug-2021

PHP Get Min or Minimum Value in Array

PHP gets the min or minimum or lowest value in the array. This tutorial has the purpose to explain to you several easy ways to find or get the min or minimum or lowest value from an array in PHP.

Here, we will take e.g. PHP get the min value in an array, find the minimum value in array PHP without a function and get the min or lowest value from an array in PHP using for loop

PHP Get the Min or Minimum value in an array

You can use the PHP min() function, which is used to find the minimum or lowest value from an array in PHP.

Before we use the take examples, you must know about the PHP min() function.

PHP min() function

The min() function is inbuilt PHP function, which is used to find or get the minimum or lowest value in an array or finds the minimum or lowest value of given specified values.

Syntax

 min(array)  
 or
 min(value1, value2, value3 … valueN)

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

Let’s take the first example, we will use the PHP min() function to find the smallest or minimum value in the array. Let’s see the example below:

<?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 program is: 1

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

Let’s take the second example, in this example, we will find or get the min or minimum number in array PHP without function. Let’s see the example below:

<?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 the third example, to find the minimum or min or smallest value in array PHP without using any function. Let’s look at the examples below:

<?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