PHP Find Min Value From Multidimensional Array

In this tutorial, you will learn how to find the lowest/min/smallest value in a multidimensional array.

How to Get Min Value in Multidimensional Array By Key and Value?

Here are two ways:

  • Finding the minimum value in a multidimensional array by key
  • Finding the minimum value in a multidimensional array by value

Finding the minimum value in a multidimensional array by key

Here, You can find the minimum value in a multidimensional array by key using the array_column() and min() function.

Here’s an example:

$data = [
['id' => 1, 'value' => 10],
['id' => 2, 'value' => 5],
['id' => 3, 'value' => 15]
];

$column = array_column($data, 'value');
$min_value = min($column);

echo $min_value; // Outputs 5

Finding the minimum value in a multidimensional array by value

Another way to find the minimum/smallest/lowest value in a multidimensional array, you can use foreach loop with sub-arrays.

Here’s an example:

$data = [
['id' => 1, 'value' => 10],
['id' => 2, 'value' => 5],
['id' => 3, 'value' => 15]
];

$min_value = $data[0]['value'];
foreach ($data as $sub_array) {
if ($sub_array['value'] < $min_value) {
$min_value = $sub_array['value'];
}
}

echo $min_value; // Outputs 5

Conclusion

That’s it, In this tutorial, you have learned how to find the minimum value in a multidimensional array using array_column(), min(), foreach loop and sub_array() function.

Recommended 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 *