Array filter function
📖 Scenario: You work in a fruit store. You have a list of fruits with their prices. You want to find only the fruits that cost more than 2 dollars.
🎯 Goal: Build a PHP program that filters an array of fruits to keep only those with prices greater than 2 dollars using the array_filter function.
📋 What You'll Learn
Create an associative array called
fruits with exact entries: 'apple' => 3, 'banana' => 1, 'cherry' => 5, 'date' => 2, 'elderberry' => 4Create a variable called
price_limit and set it to 2Use
array_filter with a callback function that keeps fruits with price greater than price_limitPrint the filtered array using
print_r💡 Why This Matters
🌍 Real World
Filtering arrays is common when you want to select only certain data from a list, like filtering products by price in an online store.
💼 Career
Understanding how to filter arrays helps in data processing tasks, backend development, and working with APIs that return lists of items.
Progress0 / 4 steps