Find the lowest value in an array with PHP

Josh Sherman
1 min read
Software Development 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!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles