Challenge - 5 Problems
Array Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of array_map with anonymous function
What is the output of this PHP code?
PHP
<?php $numbers = [1, 2, 3, 4]; $result = array_map(fn($n) => $n * 2, $numbers); print_r($result); ?>
Attempts:
2 left
💡 Hint
array_map applies the function to each element in the array.
✗ Incorrect
array_map applies the function fn($n) => $n * 2 to each element, doubling each number.
❓ Predict Output
intermediate2:00remaining
Result of array_filter with callback
What does this PHP code output?
PHP
<?php $values = [10, 15, 20, 25, 30]; $filtered = array_filter($values, fn($v) => $v > 20); print_r($filtered); ?>
Attempts:
2 left
💡 Hint
array_filter keeps elements where the callback returns true.
✗ Incorrect
Only values greater than 20 (25 and 30) remain in the filtered array, keys preserved.
🔧 Debug
advanced2:00remaining
Output of array_reduce without initial value
This PHP code is intended to sum all numbers in the array. What is the output?
PHP
<?php $nums = [1, 2, 3, 4]; $sum = array_reduce($nums, fn($carry, $item) => $carry + $item); echo $sum; ?>
Attempts:
2 left
💡 Hint
array_reduce uses the first element as the initial carry when not specified and the array is not empty.
✗ Incorrect
No error is produced. The first element (1) is used as the initial value for $carry, and the function sums the elements: 1+2+3+4=10. Providing an explicit initial value (like 0) is recommended for robustness, especially with empty arrays.
❓ Predict Output
advanced2:00remaining
Output of array_column with associative arrays
What is the output of this PHP code?
PHP
<?php $data = [ ['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob'], ['id' => 3, 'name' => 'Charlie'] ]; $names = array_column($data, 'name'); print_r($names); ?>
Attempts:
2 left
💡 Hint
array_column extracts values from a specific key in each sub-array.
✗ Incorrect
array_column returns a simple array of the 'name' values preserving numeric keys starting at 0.
🧠 Conceptual
expert2:00remaining
Why use array functions instead of loops?
Which of the following is the best reason to prefer PHP array functions like array_map, array_filter, and array_reduce over manual loops?
Attempts:
2 left
💡 Hint
Think about code readability and maintenance.
✗ Incorrect
Array functions make code easier to read and less error-prone by expressing what you want to do, not how.