Monday 2 February 2015

Array Maximum Minimum value

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_keysfunction.

$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);

0 comments:

Post a Comment