How can you filter an array in PHP based on a specific condition and display the lowest price?
To filter an array in PHP based on a specific condition and display the lowest price, you can use the `array_filter()` function to filter the array based on the condition, and then use the `min()` function to find the lowest price among the filtered values.
<?php
// Sample array of prices
$prices = [10, 15, 20, 25, 30];
// Filter the array to only include prices less than or equal to 20
$filteredPrices = array_filter($prices, function($price) {
return $price <= 20;
});
// Find the lowest price among the filtered values
$lowestPrice = min($filteredPrices);
// Display the lowest price
echo "The lowest price is: $lowestPrice";
?>
Keywords
Related Questions
- What steps can be taken to normalize file paths using functions like realpath in PHP to ensure proper file handling and avoid errors during file operations?
- Are there any best practices or guidelines for measuring and optimizing page load time in PHP?
- What are the best practices for integrating JavaScript form validation with PHP form submission handling?