Bird
Raised Fist0
PHPprogramming~20 mins

Array walk function in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Walk Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
?>
AArray ( [0] => 1 [1] => 2 [2] => 3 )
BArray ( [0] => 2 [1] => 4 [2] => 6 )
CArray ( [0] => 1 [1] => 4 [2] => 9 )
DArray ( [0] => 0 [1] => 0 [2] => 0 )
Attempts:
2 left
💡 Hint
Remember that array_walk passes values by reference if you want to modify the original array.
❓ Predict Output
intermediate
2: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 ";
});
?>
Aa:1 b:2
B1:a 2:b
Ca b
DArray
Attempts:
2 left
💡 Hint
The callback prints key and value pairs separated by a colon.
🔧 Debug
advanced
2: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);
?>
ABecause array_walk requires the callback to return a value.
BBecause array_walk cannot be used with numeric arrays.
CBecause the callback parameter $value is not passed by reference, so original array is unchanged.
DBecause print_r cannot print arrays modified by array_walk.
Attempts:
2 left
💡 Hint
Check how the callback parameters are declared and if they modify the original array.
📝 Syntax
advanced
2: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.
Aarray_walk($arr, function(&$v $k) { $v += 1; });
Barray_walk($arr, function($v, $k) { $v += 1; });
Carray_walk($arr, function(&$v, $k) { $v += 1; });
Darray_walk($arr, fn(&$v) => $v += 1);
Attempts:
2 left
💡 Hint
Check the function parameter list syntax carefully.
🚀 Application
expert
3: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);
?>
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Consider what happens when you unset elements during array_walk iteration.

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

  1. 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.
  2. Step 2: Compare with other array functions

    Sorting, merging, or removing duplicates are done by other functions like sort, array_merge, or array_unique.
  3. Final Answer:

    It applies a user-defined function to every element of an array. -> Option A
  4. 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

  1. Step 1: Recall array_walk syntax

    The correct syntax is array_walk(array, callback), where callback is a string with the function name.
  2. Step 2: Check the options

    Only array_walk($arr, 'modify'); matches the correct order and uses quotes around the callback function name.
  3. Final Answer:

    array_walk($arr, 'modify'); -> Option D
  4. 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?
$numbers = [1, 2, 3];
array_walk($numbers, function(&$value) {
    $value *= 2;
});
print_r($numbers);
medium
A. Array ( [0] => 2 [1] => 4 [2] => 6 )
B. Array ( [0] => 1 [1] => 2 [2] => 3 )
C. Array ( [0] => 0 [1] => 0 [2] => 0 )
D. Error: Cannot modify array elements

Solution

  1. Step 1: Understand the callback function

    The callback multiplies each value by 2, and the value is passed by reference (&$value), so changes affect the original array.
  2. Step 2: Apply changes to each element

    1*2=2, 2*2=4, 3*2=6, so the array becomes [2, 4, 6].
  3. Final Answer:

    Array ( [0] => 2 [1] => 4 [2] => 6 ) -> Option A
  4. Quick Check:

    Reference callback doubles values [OK]
Hint: Pass value by reference to modify array items [OK]
Common Mistakes:
  • Not using & to modify original array
  • Expecting original array unchanged
  • Confusing output format
4. Identify the error in this code snippet:
$arr = [1, 2, 3];
array_walk($arr, function($val) {
    $val += 5;
});
print_r($arr);
medium
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

  1. Step 1: Check how callback modifies array

    The callback parameter $val is passed by value, so changes inside do not affect original array.
  2. Step 2: Correct usage for modification

    Passing &$val by reference allows modifying the original array elements.
  3. Final Answer:

    Callback function parameter should be passed by reference to modify array. -> Option C
  4. 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

  1. Step 1: Identify condition and modification

    We want to add 10 to values less than 4, so check $val < 4 inside the callback.
  2. Step 2: Ensure modification affects original array

    Pass $val by reference (&$val) to change original array values.
  3. 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.
  4. Final Answer:

    array_walk($fruits, function(&$val) { if ($val < 4) $val += 10; }); -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard