PHP Get Mininum Value in Multidimensional Array

PHP get min or minimum or smallest value from multidimensional array; Through this tutorial, i am going to show you how to get or find minimum or lowest value from multidimensional array in PHP.

PHP Find Minimum Value in Multidimensional Array

Let’s take multiple examples to get min or lowest value from multidimensional array in PHP using For Loop, forEach Loop; is as follows:

  • PHP Get Minimum Value in Multidimensional Array using ForEach Loop
  • PHP Find Min/Lowest Value in Multidimensional Array using For Loop and ForEach

PHP Get Minimum Value in Multidimensional Array using ForEach Loop

Let’s see the below given example to get the minimum Value form multidimensional array using foreach loop in PHP; is as follows:

<?php 
 
function getMins($array){
  $players = array();
  foreach($array as $key => $player){
    sort($player);
    $players[$key] = $player[0];
  }
  return $players;
}

$players = $array = [[100, 200, 600],[99, 108, 849, 456],[548, 149, 7840]];

$minValues = getMins($players);

$min = $minValues[0];

foreach ($minValues as $val)
{
  
    if ($val < $min)
    {
      $min = $val;
    }
       
}


echo '<pre>'; print_r($min); echo '</pre>';
?>

PHP Find Min/Lowest Value in Multidimensional Array using For Loop and ForEach

Let’s see the below given example to get the minimum Value form multidimensional array using for loop in PHP; is as follows:

<?php 
 
function getMins($array){
  $players = array();
  foreach($array as $key => $player){
    sort($player);
    $players[$key] = $player[0];
  }
  return $players;
}

$players = $array = [[100, 200, 600],[99, 108, 849, 456],[10, 149, 7840]];

$minValues = getMins($players);

$min = $minValues[0];

    for($i=0;$i<count($minValues);$i++)
    {
        if ($minValues[$i] < $min)
        {
        $min = $minValues[$i];
        }
    } 


echo '<pre>'; print_r($min); echo '</pre>';
?>

Recommended PHP Tutorials

Leave a Comment