PHP Get Maximum Value in Multidimensional Array

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

PHP Get Maximum Value in Multidimensional Array

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

  • PHP Get Highest Value in Multidimensional Array using ForEach Loop
  • PHP Find Max/Largest Value in Multidimensional Array using For Loop

PHP Get Highest Value in Multidimensional Array using ForEach Loop

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

<?php 
 
//get the highest value from multi dimenstion array in php
$array = [[100, 200, 600],[205, 108, 849, 456],[548, 149, 784]];
$max = 0;
foreach ($array as $val)
{
    foreach($val as $key=>$val1)
    {
        if ($val1 > $max)
        {
        $max = $val1;
        }
    }       
}
print ($max);
?>

PHP Find Max/Largest Value in Multidimensional Array using For Loop

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

<?php 
 
//get the max value from multi dimenstion array in php
$array = [[100, 200, 600],[205, 108, 849, 456],[548, 149, 7840]];
$max = 0;
for($i=0;$i<count($array);$i++)
{
    for($j=0;$j<count($array[$i]);$j++)
    {
        if ($array[$i][$j] > $max)
        {
        $max = $array[$i][$j];
        }
    }       
}
print $max;
?>

Recommended PHP Tutorials

Leave a Comment