How to Get the Min and the Max Value from an Array in PHP
In today's lesson, we will see how to get the min and the max value from an array in PHP, so let's assume that we have an array of ages and we want to get the oldest and the youngest.
Get the highest value
So to get the highest value we use the PHP math function 'max()' that returns the highest value in an array.
<!DOCTYPE html>
<html>
<body>
<?php
$ages = array('John' => 18,'Carl' => 25,'Alex' => 33,'Rami' => 44);
echo "highest age : ". max($ages) . "<br>";
// result highest age : 44
?>
</body>
</html>
Get the lowest value
Finally, to get the lowest value we use the PHP math function 'min()' that returns the lowest value in an array.
<!DOCTYPE html>
<html>
<body>
<?php
$ages = array('John' => 18,'Carl' => 25,'Alex' => 33,'Rami' => 44);
echo "lowest age : ". min($ages);
// result lowest age : 18
?>
</body>
</html>