Find the largest value in an array with PHP

Last week we discussed how to find the smallest value in an array so it seemed
fitting to discuss how to find the largest value in an array. As we previously
discussed back in the day you would have to loop through an array and compare
values until you found the smallest (or in this case largest) value. As a
complement to the min() function, we also have the max() function
available:

$array   = array(99, 32, 6, 108, 144
$largest = max($array

Keep in mind that both min() and max() can work with arrays that contain
values of mixed types. YMMV as this could yield some unexpected results if
you’re not familiar with how PHP handles the comparisons of different types.

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.