Find the lowest value in an array with PHP

Finding the lowest value of an array is a pretty common task in PHP, especially
when you are dealing with things like scores or money. In the old days before
PHP 4, finding the lowest value of an array meant looping through the array
and comparing values until you had the lowest one, something like this:

$array  = array(6, 2, 10, 4, 15
$lowest = false

foreach ($array as $value) {
    if ($lowest === false || $value < $lowest) {
        $lowest = $value
    }
}

Not too terrible, but definitely not very elegant. Fortunately in PHP 4+ we
have the min() function available:

$array  = array(6, 2, 10, 4, 15
$lowest = min($array

Not much to it!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.