Bird
Raised Fist0
PHPprogramming~10 mins

Array reduce function in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the sum of all numbers in the array using array_reduce.

PHP
<?php
$numbers = [1, 2, 3, 4];
$sum = array_reduce($numbers, [1]);
echo $sum;
?>
Drag options to blanks, or click blank then click option'
Afunction($carry, $item) { return $carry + $item; }
Bfunction($carry, $item) { return $carry * $item; }
Cfunction($carry, $item) { return $item - $carry; }
Dfunction($carry, $item) { return $carry / $item; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition in the callback.
Forgetting to return the sum inside the callback.
2fill in blank
medium

Complete the code to calculate the product of all numbers in the array using array_reduce.

PHP
<?php
$numbers = [1, 2, 3, 4];
$product = array_reduce($numbers, [1], 1);
echo $product;
?>
Drag options to blanks, or click blank then click option'
Afunction($carry, $item) { return $carry + $item; }
Bfunction($carry, $item) { return $item / $carry; }
Cfunction($carry, $item) { return $carry - $item; }
Dfunction($carry, $item) { return $carry * $item; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Not setting the initial value to 1.
3fill in blank
hard

Fix the error in the callback function to correctly concatenate all strings in the array.

PHP
<?php
$words = ['Hello', ' ', 'World', '!'];
$result = array_reduce($words, [1], '');
echo $result;
?>
Drag options to blanks, or click blank then click option'
Afunction($carry, $item) { return $carry . $item; }
Bfunction($carry, $item) { return $carry - $item; }
Cfunction($carry, $item) { return $item . $carry; }
Dfunction($carry, $item) { return $carry * $item; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication operators instead of concatenation.
Concatenating in the wrong order.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values using array_reduce.

PHP
<?php
$words = ['apple', 'banana', 'cherry'];
$lengths = array_reduce($words, function($carry, $word) {
    $carry[[1]] = [2];
    return $carry;
}, []);
print_r($lengths);
?>
Drag options to blanks, or click blank then click option'
A$word
Bstrlen($word)
C$carry
Dcount($word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using $carry as key or value incorrectly.
Using count() instead of strlen().
5fill in blank
hard

Fill all three blanks to filter and sum only numbers greater than 10 using array_reduce.

PHP
<?php
$numbers = [5, 12, 7, 20, 3];
$sum = array_reduce($numbers, function($carry, $num) {
    if ($num [1] 10) {
        $carry [2]= $num;
    }
    return $carry;
}, [3]);
echo $sum;
?>
Drag options to blanks, or click blank then click option'
A>
B+
C0
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator instead of greater than.
Not initializing carry to zero.
Using wrong operator for addition.

Practice

(1/5)
1. What does the PHP function array_reduce do?
easy
A. It sorts the array elements in ascending order.
B. It combines all elements of an array into a single value using a callback function.
C. It filters elements of an array based on a condition.
D. It splits an array into smaller arrays.

Solution

  1. Step 1: Understand the purpose of array_reduce

    The function takes an array and a callback to combine elements into one value.
  2. Step 2: Compare with other array functions

    Unlike sorting or filtering, array_reduce accumulates values into one result.
  3. Final Answer:

    It combines all elements of an array into a single value using a callback function. -> Option B
  4. Quick Check:

    array_reduce = combine elements [OK]
Hint: Remember: array_reduce reduces array to one value [OK]
Common Mistakes:
  • Confusing array_reduce with sorting functions
  • Thinking it filters elements instead of combining
  • Assuming it splits arrays into parts
2. Which of the following is the correct syntax to use array_reduce in PHP?
easy
A. array_reduce(mixed $initial = null, array $array, callable $callback)
B. array_reduce(callable $callback, array $array, mixed $initial = null)
C. array_reduce(array $array, mixed $initial = null, callable $callback)
D. array_reduce(array $array, callable $callback, mixed $initial = null)

Solution

  1. Step 1: Recall the official function signature

    The correct order is array, callback, then optional initial value.
  2. Step 2: Check each option's parameter order

    Only array_reduce(array $array, callable $callback, mixed $initial = null) matches the correct order and types.
  3. Final Answer:

    array_reduce(array $array, callable $callback, mixed $initial = null) -> Option D
  4. Quick Check:

    array first, callback second, initial last [OK]
Hint: Remember: array first, callback second, initial last [OK]
Common Mistakes:
  • Swapping callback and array parameters
  • Placing initial value before callback
  • Misordering parameters causing errors
3. What is the output of the following PHP code?
$numbers = [1, 2, 3, 4];
$result = array_reduce($numbers, function($carry, $item) {
    return $carry + $item;
}, 0);
echo $result;
medium
A. 10
B. 24
C. 1234
D. Error

Solution

  1. Step 1: Understand the callback function

    The callback adds the current item to the carry (accumulated sum).
  2. Step 2: Calculate the sum step-by-step

    Starting with 0, add 1, then 2, then 3, then 4: 0+1=1, 1+2=3, 3+3=6, 6+4=10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Sum of 1+2+3+4 = 10 [OK]
Hint: Sum array with array_reduce and initial 0 [OK]
Common Mistakes:
  • Forgetting initial value causes null carry
  • Concatenating instead of adding numbers
  • Expecting product instead of sum
4. Identify the issue in this PHP code using array_reduce:
$values = [];
$result = array_reduce($values, function($carry, $item) {
    return $carry + $item;
});
echo $result;
medium
A. Callback function has wrong parameter order.
B. array_reduce cannot be used on empty arrays.
C. Missing initial value causes $carry to be null.
D. The echo statement is missing parentheses.

Solution

  1. Step 1: Check initial value usage

    No initial value is given, so $carry starts as null.
  2. Step 2: Understand effect on empty array

    With empty array and no initial, array_reduce returns null without calling the callback.
  3. Final Answer:

    Missing initial value causes $carry to be null. -> Option C
  4. Quick Check:

    Initial value needed for empty arrays [OK]
Hint: Always provide initial value to avoid null carry [OK]
Common Mistakes:
  • Assuming callback parameters are reversed
  • Thinking array_reduce fails on empty arrays without initial
  • Ignoring PHP syntax for echo
5. Given an array of strings ['apple', 'banana', 'cherry'], how can you use array_reduce to create a single string with all fruits separated by commas (e.g., 'apple,banana,cherry')?
hard
A. Use array_reduce with callback concatenating carry and item with a comma, initial value as empty string.
B. Use array_reduce with callback adding carry and item as numbers, initial value 0.
C. Use array_reduce with callback returning only the last item, no initial value.
D. Use array_reduce with callback multiplying carry and item, initial value 1.

Solution

  1. Step 1: Understand the goal

    We want to join strings with commas into one string.
  2. Step 2: Design the callback

    Callback should add a comma between carry and item, starting with empty string.
  3. Step 3: Avoid leading comma

    Use a condition to avoid comma before first item or trim later.
  4. Final Answer:

    Use array_reduce with callback concatenating carry and item with a comma, initial value as empty string. -> Option A
  5. Quick Check:

    Concatenate strings with commas using array_reduce [OK]
Hint: Concatenate strings with commas, start with empty string [OK]
Common Mistakes:
  • Adding strings as numbers causing errors
  • Not providing initial value causing null carry
  • Using multiplication on strings
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