PHP Get Min/Minimum Value in Array

PHP Get Min/Minimum Value in Array

Sometimes, you may need to find the minimum value from an array in PHP. This is a common task in programming, and fortunately, PHP provides several built-in functions to help you achieve this.

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.

How to Find/Get Min/Minimum/Lowest value From an array

Below given different ways to find the minimum value from an array in PHP.

  • Method 1: Using the min() function
  • Method 2: Using a loop
  • Method 3: Using the sort() function

Method 1: Using the min() function

The simplest and most straightforward way to find the minimum value from an array in PHP is to use the built-in min() function. This function takes an array as its argument and returns the minimum value from the array.

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, you 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 2 – Find min value from an array without using PHP array functions

Let’s take the second example, in this example, you 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

Method 2: Using a loop

Another way to find the minimum value from an array is to use a loop. This method is more complex than using the min() function, but it gives you more control over the process.

Here’s an example:

$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 "The minimum value is: " . $min;

Output:

The minimum value is: 1

In this example, you initialize the $min variable to the first element of the array. You then loop through the remaining elements of the array and compare each element with the current value of $min. If the current element is smaller than $min, then update the value of $min to the current element.

Method 3: Using the sort() function

Another way to find the minimum value from an array is to sort the array in ascending order and then get the first element of the sorted array.

Here’s an example:

$numbers = array(5, 3, 8, 2, 9, 1);
sort($numbers);
$min = $numbers[0];
echo "The minimum value is: " . $min;

Output:

The minimum value is: 1

In this example, you use the sort() function to sort the array in ascending order. After sorting the array, and get the first element of the array, which is the minimum value.

Conclusion

Finding the minimum value from an array in PHP is a common task in programming. In this article, you have discussed three different ways to find the minimum value from an array in PHP. You can choose the method that suits your needs and preferences. The simplest and most straightforward method is to use the min() function. If you want more control over the process, you can use a loop. Finally, if you don’t mind modifying the original array, you can use the sort() function to find the minimum value.

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *