Challenge - 5 Problems
Array Walk Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using array_walk?
Consider the following PHP code. What will it output when run?
PHP
<?php $arr = [1, 2, 3]; array_walk($arr, function(&$value, $key) { $value *= 2; }); print_r($arr); ?>
Attempts:
2 left
💡 Hint
Remember that array_walk passes values by reference if you want to modify the original array.
✗ Incorrect
The function multiplies each element by 2 by reference, so the original array is changed.
❓ Predict Output
intermediate2:00remaining
What does this PHP code print?
Look at this PHP code using array_walk. What will be printed?
PHP
<?php $arr = ['a' => 1, 'b' => 2]; array_walk($arr, function($value, $key) { echo "$key:$value "; }); ?>
Attempts:
2 left
💡 Hint
The callback prints key and value pairs separated by a colon.
✗ Incorrect
array_walk calls the callback for each element, printing key and value with a colon.
🔧 Debug
advanced2:00remaining
Why does this PHP code cause an error?
This PHP code tries to modify array elements using array_walk but causes an error. Why?
PHP
<?php $arr = [1, 2, 3]; array_walk($arr, function($value) { $value *= 2; }); print_r($arr); ?>
Attempts:
2 left
💡 Hint
Check how the callback parameters are declared and if they modify the original array.
✗ Incorrect
Without passing $value by reference, changes inside the callback do not affect the original array.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in this PHP array_walk usage?
Identify which code snippet will cause a syntax error when using array_walk.
Attempts:
2 left
💡 Hint
Check the function parameter list syntax carefully.
✗ Incorrect
Option A misses a comma between parameters, causing a syntax error.
🚀 Application
expert3:00remaining
How many items are in the array after this PHP code runs?
Given this PHP code using array_walk, how many items does the array have after execution?
PHP
<?php $arr = [10, 20, 30]; array_walk($arr, function(&$v, $k) use (&$arr) { if ($v > 15) { unset($arr[$k]); } }); print_r($arr); ?>
Attempts:
2 left
💡 Hint
Consider what happens when you unset elements during array_walk iteration.
✗ Incorrect
Elements with values 20 and 30 are removed, leaving only the element with value 10.