PHP Get Highest Value in Multidimensional Array

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.

You should also read this array posts 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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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