0
0
PHPprogramming~15 mins

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

Choose your learning style9 modes available
Array walk function
📖 Scenario: You have a list of fruits with their prices. You want to increase each fruit's price by 10% to prepare for a sale.
🎯 Goal: Use the PHP array_walk function to update each fruit's price by increasing it by 10%.
📋 What You'll Learn
Create an associative array called fruits with exact keys and values
Create a function called increasePrice that increases the price by 10%
Use array_walk with increasePrice to update the prices
Print the updated fruits array
💡 Why This Matters
🌍 Real World
Increasing prices or applying discounts to product lists is common in online stores and inventory management.
💼 Career
Understanding how to manipulate arrays with functions like <code>array_walk</code> is useful for backend PHP developers working with data transformations.
Progress0 / 4 steps
1
Create the fruits array
Create an associative array called fruits with these exact entries: 'apple' => 100, 'banana' => 50, 'cherry' => 75.
PHP
Need a hint?

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

2
Create the increasePrice function
Create a function called increasePrice that takes two parameters: &$price and $key. Inside the function, increase $price by 10% using $price *= 1.1;.
PHP
Need a hint?

Remember to pass $price by reference using & so the original array updates.

3
Use array_walk to update prices
Use array_walk with the $fruits array and the increasePrice function to update each fruit's price.
PHP
Need a hint?

Use the function name as a string in array_walk.

4
Print the updated fruits array
Print the $fruits array using print_r($fruits); to show the updated prices.
PHP
Need a hint?

Use print_r to display the array in a readable format.