• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php min – Find Minimum Value of Array in php

php min – Find Minimum Value of Array in php

December 13, 2021 Leave a Comment

To find the minimum value of an array in php, we can use the php min() function.

$min = min($values)

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:

$sample_array = array(0,4,1,-5)

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 First Element of Array in php
  • 2.  Using strtolower() in php to Convert String to Lowercase
  • 3.  php array_filter() – Filter Array Elements with Function in php
  • 4.  php floor – Round Down and Find the Floor of a Number in php
  • 5.  php cos – Find Cosine of Number in Radians Using php cos() Function
  • 6.  php sleep() Function – Pause Execution of Code in php
  • 7.  php is_string() Function – Check if Variable is String in php
  • 8.  php str_split() – Convert String into Array of Equal Length Substrings
  • 9.  Get Current Month of Date in php
  • 10.  Get Last Element of Array in php

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.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy