PHP program to find the maximum and the minimum in array
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 // Returns maximum in array function getMax($array) { $n = count($array); $max = $array[0]; for ($i = 1; $i < $n; $i++) if ($max < $array[$i]) $max = $array[$i]; return $max; } // Returns maximum in array function getMin($array) { $n = count($array); $min = $array[0]; for ($i = 1; $i < $n; $i++) if ($min > $array[$i]) $min = $array[$i]; return $min; } // Driver code $array = array(1, 2, 3, 4, 5); echo(getMax($array)); echo("\n"); echo(getMin($array)); ?> |
chevron_right
filter_none
Output:
5 1
Approach 2 (Using Library Functions) : We use library functions to find minimum and maxium.
- 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 $array = array(1, 2, 3, 4, 5); echo(max($array)); echo("\n"); echo(min($array)); ?> |
chevron_right
filter_none
Output:
5 1
Recommended Posts:
- PHP program to find missing element(s) from an array
- PHP program to find the Standard Deviation of an array
- Program to Insert new item in array on any position in PHP
- PHP program to add item at the beginning of associative array
- Program to remove empty array elements in PHP
- Program to find the number of days between two dates in PHP
- PHP program to find the length of the last word in string
- PHP | Program to delete an element from array using unset() function
- Find the min/max element of an Array using JavaScript
- How to find the index of all occurrence of elements in an array using JavaScript ?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- How to get the elements of one array which are not present in another array using JavaScript?
- How to check whether an array is subset of another array using JavaScript ?
- What is the difference between array_merge and array + array in PHP?
- Maximum execution time taken by a PHP Script
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



