📖 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
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
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
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
Hint
Use print_r to display the array in a readable format.
Practice
(1/5)
1. What does the PHP function array_walk do?
easy
A. It applies a user-defined function to every element of an array.
B. It sorts the array in ascending order.
C. It merges two arrays into one.
D. It removes duplicate values from an array.
Solution
Step 1: Understand the purpose of array_walk
The function array_walk is designed to apply a callback function to each element of an array.
Step 2: Compare with other array functions
Sorting, merging, or removing duplicates are done by other functions like sort, array_merge, or array_unique.
Final Answer:
It applies a user-defined function to every element of an array. -> Option A
Quick Check:
array_walk = apply function to each element [OK]
Hint: array_walk runs a function on each array item [OK]
Common Mistakes:
Confusing array_walk with sorting functions
Thinking it merges arrays
Assuming it removes duplicates
2. Which of the following is the correct syntax to use array_walk with a callback function named modify on an array $arr?
easy
A. array_walk('modify', $arr);
B. array_walk(modify, $arr);
C. array_walk($arr, modify);
D. array_walk($arr, 'modify');
Solution
Step 1: Recall array_walk syntax
The correct syntax is array_walk(array, callback), where callback is a string with the function name.
Step 2: Check the options
Only array_walk($arr, 'modify'); matches the correct order and uses quotes around the callback function name.
Final Answer:
array_walk($arr, 'modify'); -> Option D
Quick Check:
array_walk(array, 'function') syntax [OK]
Hint: array_walk takes array first, then callback as string [OK]
Common Mistakes:
Swapping the order of parameters
Omitting quotes around callback name
Passing callback without quotes
3. What will be the output of the following PHP code?
A. The array must be passed by reference to array_walk.
B. array_walk requires a named function, not an anonymous function.
C. Callback function parameter should be passed by reference to modify array.
D. print_r cannot print arrays modified by array_walk.
Solution
Step 1: Check how callback modifies array
The callback parameter $val is passed by value, so changes inside do not affect original array.
Step 2: Correct usage for modification
Passing &$val by reference allows modifying the original array elements.
Final Answer:
Callback function parameter should be passed by reference to modify array. -> Option C
Quick Check:
Use & in callback to change array items [OK]
Hint: Use & in callback parameter to modify array elements [OK]
Common Mistakes:
Ignoring reference in callback parameter
Thinking array_walk needs array by reference
Believing anonymous functions are not allowed
5. Given the array $fruits = ['apple' => 2, 'banana' => 3, 'cherry' => 5];, how can you use array_walk to add 10 to each value only if the value is less than 4?
hard
A. array_walk($fruits, function($val) { if ($val < 4) $val += 10; });
B. array_walk($fruits, function(&$val) { if ($val < 4) $val += 10; });
C. array_walk($fruits, function(&$val, $key) { if ($key < 4) $val += 10; });
D. array_walk($fruits, function(&$val, $key) { if ($val > 4) $val += 10; });
Solution
Step 1: Identify condition and modification
We want to add 10 to values less than 4, so check $val < 4 inside the callback.
Step 2: Ensure modification affects original array
Pass $val by reference (&$val) to change original array values.
Step 3: Verify options
array_walk($fruits, function(&$val) { if ($val < 4) $val += 10; }); correctly uses reference and condition. array_walk($fruits, function($val) { if ($val < 4) $val += 10; }); lacks reference, so no change. array_walk($fruits, function(&$val, $key) { if ($key < 4) $val += 10; }); checks key instead of value. array_walk($fruits, function(&$val, $key) { if ($val > 4) $val += 10; }); adds 10 when value is greater than 4, which is wrong.
Final Answer:
array_walk($fruits, function(&$val) { if ($val < 4) $val += 10; }); -> Option B
Quick Check:
Reference + condition on value < 4 [OK]
Hint: Use & and if condition inside callback to selectively modify [OK]
Common Mistakes:
Not passing value by reference
Checking key instead of value
Using wrong comparison operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.