0
0
PHPprogramming~15 mins

Array map function in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Array map function
📖 Scenario: You work in a bakery that wants to update the prices of some pastries. You have a list of prices, and you want to increase each price by 10% to cover new costs.
🎯 Goal: Use the array_map function in PHP to create a new list of updated prices with a 10% increase.
📋 What You'll Learn
Create an array called prices with the values 2.50, 3.00, 4.25, 5.00
Create a variable called increase_rate and set it to 0.10
Use array_map with a function that increases each price by increase_rate
Print the new array of updated prices
💡 Why This Matters
🌍 Real World
Updating prices in a list is common in stores and online shops when costs change.
💼 Career
Knowing how to use <code>array_map</code> helps you process lists of data efficiently in PHP, a skill useful for web developers.
Progress0 / 4 steps
1
Create the initial array of prices
Create an array called prices with these exact values: 2.50, 3.00, 4.25, 5.00.
PHP
Need a hint?

Use square brackets [] to create the array and separate values with commas.

2
Set the increase rate
Create a variable called increase_rate and set it to 0.10 to represent a 10% increase.
PHP
Need a hint?

Use the = sign to assign the value to the variable.

3
Use array_map to increase prices
Use array_map with a function that takes each price and returns the price increased by increase_rate. Store the result in a variable called updated_prices.
PHP
Need a hint?

Use an anonymous function with use ($increase_rate) to access the variable inside the function.

4
Print the updated prices
Print the updated_prices array using print_r to see the new prices.
PHP
Need a hint?

Use print_r($updated_prices); to display the array.