Array Maximum Minimum value

We can calculate maximum value among the elements of an array by using max function. Here is a simple example.
$ar=array(25,26,47,14,29,58,15);
echo max($ar); // output is 58
Another example
$ar=array(1=>15,3=>25,4=>13,5=>10);
echo max($ar);// output is 25

Key of the maximum value in array

To get the key of the maximum value of the array we have to use array_keys function.
$ar=array(1=>15,3=>25,4=>13,5=>10);
echo max($ar);
$b= array_keys($ar,max($ar));
echo $b[0];
We can use this in string arrays also.
$a=array('Welcome',0);
echo max($a); //optput is Welcome

Min function

We can use min function also in same way to get the minimum value from the array. Here is the example.
$ar=array(25,26,47,14,29,58,15);
echo min($ar);

Example with two max value

<?Php
$value= array(2,5,6,8,9,4,9,1);
echo "Maximum value = ".max($value);
?>
Output is
Maximum value = 9

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


Google+

psjlakshmi

10-11-2014

if there are duplicate values or if the minimum value is twice in the array?
smo

27-03-2015

example with two maximum value is added

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer