PHP Get Highest Value in Multidimensional Array
Php 11-Aug-2021

PHP Get Highest Value in Multidimensional Array

PHP get or find the highest or maximum value in a multidimensional array. In this tutorial, you will learn how to get the maximum or highest value from the multidimensional array in PHP.

This tutorial shows you two easy ways to find the highest or max value from the multidimensional array in PHP.

1. PHP Get Highest Value in Multidimensional Array

Let’s take the first example, to get the highest value in the multidimensional array using foreach loop in PHP.

<?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);
?>

The output of the above code is: 784

2. PHP Find Max/Largest Value in Multidimensional Array

Let’s take the second example, to get the highest value in the multidimensional array using for loop in PHP.

<?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;
?>

The output of the above code is: 7840