Get Max Value From Array in PHP

PHP get or find max or maximum or highest or largest value from array; Through this tutorial, i am going to show you how to find or get the max or maximum or highest value from an array in PHP.

PHP Get the Max or Maximum value in an array

Using the PHP max() function, you can find or get maximum or highest or largest value from an array in PHP.

Now, i will take some examples using php max() function, for loop and without max() function to get or find max or highest value from array in PHP.

  • Example – 1 Get max value in array PHP using max() function
  • Example 2 – Find highest value from an array without using PHP array functions
  • Example 3 – PHP get max value in array using for loop

Example – 1 Get max value in array PHP using max() function

Let’s see the following example to get or find the max or highest value in the array in php; as follows:

<?php
 
$array = [1, 10, 50, 40, 2, 15, 100];
 
$res = max($array);
 
print_r($res);
 
?> 
The output of the above php code is: 100

Example 2 – Find highest value from an array without using PHP array functions

Let’s see the second example for find or get the max or highest number in array PHP without function; as follows:

<?php
 
$array = array(1000,400,10,50,170,500,45);
 
$max = $array[0];
 
foreach($array as $key => $val){
 
    if($max < $val){
 
        $max = $val;
         
    }
}   
 
print $max;
 
?> 
 The output of the above program is: 1000

Example 3 – PHP get max value in array using for loop

Let’s take third example for find the highest or max value in array PHP without using any function; as follows:

<?php 
 
$array = array(500, 20, 55 ,165, 456, 78, 75);
 
 
$max = 0;
 
for($i=0;$i<count($arr);$i++)
 {
    if ($arr[$i] > $max)
    {
        $max = $arr[$i];
    }
}
 
print $max;
 
?>
  The output of the above program is: 500

Recommended PHP Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *