PHP Get Highest Value in Multidimensional Array

Working with multidimensional arrays in PHP is a common task. Sometimes, you may need to find the highest 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:

How to Find/Get the Highest Value in Multidimensional Array?

Here are some simple methods and examples:

  • Method 1: Using a ForEach Loop with Max
  • Method 2: Using the max function and array_merge
  • Method 3: Using array_reduce and max
  • Method 4: Using for loop with max

Method 1: Using a ForEach Loop with Max

A loop is one of the simplest methods to find the highest value in a multidimensional array. Here’s an example code:

// Define a sample multidimensional array
$array = array(
array(5, 10, 15),
array(20, 25, 30),
array(35, 40, 45),
);

// Initialize the maximum variable to the first element
$max = $array[0][0];

// Loop through each element in the multidimensional array
foreach($array as $sub_array) {
foreach($sub_array as $element) {
if($element > $max) {
$max = $element;
}
}
}

// Print the maximum value
echo $max;

Output:

45

Method 2: Using the max function and array_merge

You can also use php built-in max() function to find the highest value in a multidimensional array Here’s an example code:

// Define a sample multidimensional array
$array = array(
array(5, 10, 15),
array(20, 25, 30),
array(35, 40, 45),
);

// Merge all sub-arrays into a single array
$merged_array = call_user_func_array('array_merge', $array);

// Find the maximum value using the max function
$max = max($merged_array);

// Print the maximum value
echo $max;

Output:

45

Method 3: Using array_reduce and max

Another method is named array_reduce() to find the highest value in a multidimensional array. Here’s an example code:

// Define a sample multidimensional array
$array = array(
array(5, 10, 15),
array(20, 25, 30),
array(35, 40, 45),
);

// Define a callback function to find the maximum value
$callback = function($carry, $item) {
return max($carry, max($item));
};

// Use array_reduce to apply the callback function to each sub-array
$max = array_reduce($array, $callback, PHP_INT_MIN);

// Print the maximum value
echo $max;

Output:

45

Method 4: Using for loop with max

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

Conclusion

In this tutorial, you have learned how to get the maximum or highest value from the multidimensional array in PHP.

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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