Array Maximum Minimum value

We can calculate maximum value among the elements of an array by using max function. Here is a simple example.
Maximum value of Array
echo max($ar); // output is 58

  • Maximum & Minimum value & key of Array

Another example
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.
We can also search for the value and return the key by using array_search()

echo max($ar); // output 25 
echo "<br>";
$b= array_keys($ar,max($ar));
echo $b[0]; // output 3 

echo "<br>"; // using array_search()
echo $key; // output 3
We can use this in string arrays also.
echo max($a); //optput is Welcome
echo max($ar); // output is na

Min function

We can use min function also in same way to get the minimum value from the array. Here is the example.
echo min($ar);

Example with two max value

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


Here is an array of students where name of the student is key and mark is stored as value. Collect the highest and lowest mark ( values here ) along with the student names ( keys here ) . Used array_search() to collect the key ( student name )

echo "highest mark : ". max($student); // output 25
echo "<br>";
echo "Student got highest mark:".array_search(max($student),$student);
echo "<br>";
echo "Lowest mark : ". min($student); // output 13
echo "<br>";
echo "Student got Lowest mark:".array_search(min($student),$student);
Output is here.
highest mark : 25
Student got highest mark : Geek
Lowest mark : 13
Student got Lowest mark : Alex
Joining Two Arrays by array_merge
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net



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


    example with two maximum value is added


    excellent, thank you!

    Post your comments , suggestion , error , requirements etc here

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer