📖 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
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
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
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
Hint
Use print_r($updated_prices); to display the array.
Practice
(1/5)
1. What does the PHP function array_map do?
easy
A. Deletes elements from an array based on a condition
B. Merges two or more arrays into one
C. Sorts the elements of an array in ascending order
D. Applies a function to each element of an array and returns a new array
Solution
Step 1: Understand the purpose of array_map
The function applies a given callback function to each element of the input array.
Step 2: Identify the output of array_map
It returns a new array with the results of the callback function applied to each element.
Final Answer:
Applies a function to each element of an array and returns a new array -> Option D
Quick Check:
array_map = apply function to array elements [OK]
Hint: Remember: array_map transforms each item, returns new array [OK]
Common Mistakes:
Thinking array_map modifies the original array
Confusing array_map with array_filter
Assuming array_map merges arrays
2. Which of the following is the correct syntax to use array_map in PHP?
easy
A. array_map(function, array);
B. array_map(array, function);
C. array_map->function(array);
D. array_map(function => array);
Solution
Step 1: Recall the function signature
The correct syntax is array_map(callback_function, array_to_process).
Step 2: Match the options with the syntax
array_map(function, array); matches the correct order: function first, then array.
Final Answer:
array_map(function, array); -> Option A
Quick Check:
array_map(callback, array) syntax [OK]
Hint: Function comes first, array second in array_map [OK]
Common Mistakes:
Swapping the order of function and array
Using arrow or assignment syntax incorrectly
Using object method syntax instead of function call