View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given an array of integers, find the maximum and minimum in it.
Examples:
Input : arr[] = {2, 3, 1, 6, 7} Output : Maximum integer of the given array:7 Minimum integer of the given array:1 Input : arr[] = {1, 2, 3, 4, 5} Output : Maximum integer of the given array : 5 Minimum integer of the given array : 1
Approach 1 [Simple] : We
simply traverse through the array, find its maximum and minimum.
PHP
Output:
5 1
Approach 2 [Using Library Functions] : We use library functions to find minimum and maximum.
- Max[]:max[]
returns the parameter value considered “highest” according to standard comparisons. If multiple values of different types evaluate as equal [e.g. 0 and ‘abc’] the first provided to the function will be returned.
- Min[]:min[] returns the parameter value considered “lowest” according to standard comparisons. If multiple values of different types evaluate as equal [e.g. 0 and ‘abc’] the first provided to the function will be returned.
PHP
Output:
5 1
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.