- php min – Find Minimum Value of Array in php
- php min() for Arguments of Different Types
- Using php min() to Find the Minimum Between Dates
- Other Articles You'll Also Like:
- About The Programming Expert
- PHP Get Min/Minimum Value in Array
- How to Find/Get Min/Minimum/Lowest value From an array
- Method 1: Using the min() function
- Syntax
- 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
- Method 2: Using a loop
- Method 3: Using the sort() function
- Conclusion
- PHP min() Function
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- PHP get the minimum number in an array
- Take your skills to the next level ⚡️
- About
- Search
- Tags
php min – Find Minimum Value of Array in php
To find the minimum value of an array in php, we can use the php min() function.
The min function returns the “lowest” value according to standard comparisons in php. If there are multiple values which have the same value, then the first one will be returned.
To get the minimum of an array in php, we use the php min() function.
Let’s say we have the following array of integers:
We can easily get the minimum of this array by calling the php min() function.
$sample_array = array(0,4,1,-5); echo min($sample_array); // -5
php min() for Arguments of Different Types
The php min() function is able to take arguments of different types and allows us to compare between integers, strings, and arrays.
If you want to use the php min() function to compare an array with multiple types, you need to understand how the min() function works with these different types.
For strings, the min() function assigns a value of 0.
So if I call the php min() function with the following arguments, the returned value will be “example string”, because 0 is less than 1.
echo min("example string",1); // "example string"
If we want to compare arrays with other arrays or values, then there are a few things we need to keep in mind.
First, the longer array is always greater than a shorter array. Second, if they arrays are the same length, then we go from left to right and if there is a difference in elements, the array which contains this element is returned as the minimum. Third, if an array is compared against an integer or string, the array is always bigger.
We can see this below in the following examples of using the php min() method with arrays:
echo json_encode(min(array(0,3,4),array(0,5,1))); // [0,3,4] because 3 < 5 echo json_encode(min(array(0,3,4),array(-10,0,5,1))); // [0,3,4] because the array is smaller than [-10,0,5,1] echo json_encode(min(array(0,3,4),"example_string")); // "example_string" because arrays are always bigger
If you want to compare boolean variables, you can do this, but the results will not necessarily make sense.
Using php min() to Find the Minimum Between Dates
The min() function can also find the minimum between dates. All we need to do is pass our dates to the function.
$dt1 = new DateTime('2021-12-07 18:34', new DateTimeZone('UTC')); $dt2 = new DateTime('2021-12-03 11:34', new DateTimeZone('UTC')); echo min($dt1,$dt2)->format(DateTime::RFC3339) // 2021-12-03T11:34:00+00:00
Hopefully this post has been beneficial for you to understand how you can calculate the minimum of an array in php with the php min() function.
Other Articles You'll Also Like:
- 1. Get Last Day of Month in php
- 2. Using php to Copy Files Easily from One Directory to Another
- 3. Remove First Character from String Using php
- 4. Remove HTML Tags from String in php
- 5. PHP random_int() Function – Generate Random Integers in PHP
- 6. Delete Cookie Using php
- 7. php array_slice() – Get Slice of Elements from Array
- 8. Add Months to Date in php
- 9. php array_fill() – Create Array of Length N with Same Value
- 10. php Random Number Generator with rand Function
About The Programming Expert
The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.
Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.
At the end of the day, we want to be able to just push a button and let the code do it’s magic.
You can read more about us on our about page.
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
- 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:
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:
$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:
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.
$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;
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.
$numbers = array(5, 3, 8, 2, 9, 1); sort($numbers); $min = $numbers[0]; echo "The minimum value is: " . $min;
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.
PHP min() Function
The min() function returns the lowest value in an array, or the lowest value of several specified values.
Syntax
Parameter Values
Parameter | Description |
---|---|
array_values | Required. Specifies an array containing the values |
value1,value2. | Required. Specifies the values to compare (must be at least two values) |
Technical Details
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
PHP get the minimum number in an array
Posted on Aug 15, 2022
To get the minimum or lowest number in a PHP array, you need to call the min() function.
The min() function in PHP is used to get the lowest value in an array or in several specified values.
The return value depends on the parameters you passed to the function.
When you pass an array of numbers, this function returns the minimum number:
While the function works with other types like string and boolean , it’s not recommended because the comparison can produce unpredictable results.
Consider the following examples:
Comparing non-number values using the min() function is kind of nonsense, so it shouldn’t happen if you code your application right.
And that’s how you find the minimum number in PHP 👍
Take your skills to the next level ⚡️
I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter
Tags
Click to see all tutorials tagged with: