0
0
PHPprogramming~10 mins

Array reduce function in PHP - Interactive Code Practice

Choose your learning style9 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.