How to Find the Smallest Number in an Array in PHP

PHP


In this article, we show how to find the smallest number in an array in PHP.

To find the smallest number in an array in PHP, we use the min() function.

Let's say we have the array of numbers below:

$numbers= array(170, 210, 103, 375, 315, 470, 255);

Let's say these numbers represent prices of airline tickets that a user looks up. To find the cheapest price, we need to be able to pick out the smallest number in the array.

We use the min() function to find the smallest number in the array.

The general form to find the smallest number in an array is:

$smallest= max($array_name);

where $array_name is the name of the array and $smallest is the variable which contains the lowest numerical value of the array.

For the above array we created, the code to find the smallest number is:

$smallest= min($numbers);

Actual PHP Output

The smallest value in the array is 103

Finding the smallest value in an array is very crucial for many applications. With finding the smallest value, we can find the cheapest price, meal, airline tickets, hotel, taxi fare, etc.

For a tutorial on finding the largest number in an array, see How to Find the Largest Number in an Array in PHP.

HTML Comment Box is loading comments...