0
0
PHPprogramming~20 mins

Why array functions matter in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
?>
AArray ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
BArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
CArray ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )
DSyntax error
Attempts:
2 left
💡 Hint
array_map applies the function to each element in the array.
Predict Output
intermediate
2: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);
?>
AArray ( [0] => 10 [1] => 15 [2] => 20 )
BFatal error: Uncaught TypeError
CArray ( [0] => 10 [1] => 15 [2] => 20 [3] => 25 [4] => 30 )
DArray ( [3] => 25 [4] => 30 )
Attempts:
2 left
💡 Hint
array_filter keeps elements where the callback returns true.
🔧 Debug
advanced
2: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;
?>
AFatal error: Uncaught TypeError
BNotice: Undefined variable: carry
COutput: 10
DWarning: array_reduce() expects parameter 3 to be mixed, none given
Attempts:
2 left
💡 Hint
array_reduce uses the first element as the initial carry when not specified and the array is not empty.
Predict Output
advanced
2: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);
?>
AArray ( [0] => Alice [1] => Bob [2] => Charlie )
BArray ( [1] => Alice [2] => Bob [3] => Charlie )
CSyntax error
DArray ( [id] => name )
Attempts:
2 left
💡 Hint
array_column extracts values from a specific key in each sub-array.
🧠 Conceptual
expert
2: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?
AThey allow modifying the original array without copying
BThey provide clearer, shorter code that expresses intent and reduces errors
CThey always run faster than loops in every situation
DThey automatically parallelize code for better performance
Attempts:
2 left
💡 Hint
Think about code readability and maintenance.