Current Location: Home> Latest Articles> How to Combine the min and array_filter Functions in PHP for Efficient Minimum Value Filtering?

How to Combine the min and array_filter Functions in PHP for Efficient Minimum Value Filtering?

gitbox 2025-06-11

In PHP development, when dealing with array data, it is common to need to filter out elements that meet certain conditions and find the minimum value among them. This article introduces how to combine the array_filter and min functions to efficiently and concisely filter the minimum value.


1. Understanding the Functions array_filter and min

  • array_filter: Used to iterate through an array and filter out elements that meet a given condition based on a custom callback function, returning a new array.

  • min: Returns the minimum value from an array or from multiple parameters.

Typically, if you use min directly, it cannot filter out elements that do not meet the condition. However, array_filter can be used to filter the elements first, and then applying min on the result allows you to get the minimum value.


2. Example Scenario

Assume you have a set of data representing the prices of certain products, and you only want to get the minimum price among the products that cost more than 100.

<?php
$prices = [150, 50, 200, 120, 80, 300];
<p>// Use array_filter to filter elements with prices greater than 100<br>
$filtered = array_filter($prices, function($price) {<br>
return $price > 100;<br>
});</p>
<p>// Use min to get the minimum value from the filtered array<br>
$minPrice = min($filtered);</p>
<p>echo "The minimum price of products greater than 100 is: " . $minPrice;<br>
?><br>

Output:

The minimum price of products greater than 100 is: 120

3. Considerations

3.1 Handling Empty Arrays After Filtering

If the filtering condition is too strict, resulting in an empty array, the min function will throw a warning, which may cause the program to crash.

It is recommended to check whether the filtered array is empty first:

<?php
$filtered = array_filter($prices, function($price) {
    return $price > 500; // Strict condition, might return no results
});
<p>if (!empty($filtered)) {<br>
$minPrice = min($filtered);<br>
echo "The minimum value that meets the condition is: " . $minPrice;<br>
} else {<br>
echo "No elements meet the condition.";<br>
}<br>
?><br>


4. Using with Associative Arrays

If the array is associative and you need to filter based on values:

<?php
$products = [
    'A' => 150,
    'B' => 50,
    'C' => 200,
    'D' => 120,
    'E' => 80,
    'F' => 300,
];
<p>$filtered = array_filter($products, fn($price) => $price > 100);</p>
<p>if (!empty($filtered)) {<br>
$minPrice = min($filtered);<br>
$minKey = array_search($minPrice, $filtered);<br>
echo "The minimum price greater than 100 is $minPrice for the product with ID $minKey";<br>
} else {<br>
echo "No products meet the condition.";<br>
}<br>
?><br>


5. Summary

  • First, use array_filter to filter the elements that meet the condition.

  • Then, use min to find the minimum value from the filtered array.

  • Make sure to handle empty arrays to avoid runtime errors.

  • When dealing with associative arrays, use array_search to find the corresponding key name.

This method is simple, clear, and efficient, making it suitable for most scenarios where filtering and finding the minimum value is required.