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.