How to Find Minimum and Maximum Values in an Array in PHP?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
How to Find Minimum and Maximum Values in an Array in PHP

Hello Dev’s, Today now in this blog, I will share you how to get minimum value and maximum value from an aaray in php. If we use “min()” function then it returns the lowest value from that array. If we at least two parameters are provided,then min() returns the smallest of these values.

And for the max() function is returns us the highest value from that array in php. So we can find maximum value from the array.

Solution 1 : Minimum Value
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function getMinValue()
{
    $myArray = [100,20,30,50,220,3,22,56];

    $minValue = min($myArray);

    dd($minValue);
}
Solution 2 : Minimum Value using foreach loop
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function getMinValue()
{
    $myArray = [1000,500,10,56,560,6,4];

    $minValue = $myArray[0];

    // get lowest or minimum value in array using foreach loop

    foreach($myArray as $key => $val){
        if($minValue > $val){
            $minValue = $val;
        }
    }   
    dd($minValue);
}
Solution 1 : Maximum Value
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function getMaxValue()
{
    $myArray = [100,20,30,50,220,3,22,56];

    $maxValue = max($myArray);

    dd($maxValue);
}
Solution 2 : Maximum Value using foreach loop
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function getMaxValue()
{
    $array = [1000,500,10,56,560,6,4];

    $maxValue = $array[0];

    // get highest or maximum value in array using foreach loop

    foreach($array as $key => $val){
        if($maxValue < $val){
            $maxValue = $val;
        }
    }   
    dd($maxValue);
}

Read also : How To Set Bcc And Cc Mail Address In Laravel Mail?

I hope it will help you. Also you can follow us on Facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →