Complete the code to filter numbers greater than 5 using array_filter and a closure.
<?php $numbers = [2, 5, 8, 1, 10]; $filtered = array_filter($numbers, function($num) { return $num [1] 5; }); print_r($filtered); ?>
The closure checks if each number is greater than 5 using the '>' operator.
Complete the code to double each number in the array using array_map and a closure.
<?php $numbers = [1, 2, 3, 4]; $doubled = array_map(function($num) { return $num [1] 2; }, $numbers); print_r($doubled); ?>
The closure multiplies each number by 2 to double it.
Fix the error in the closure to correctly filter even numbers using array_filter.
<?php $numbers = [1, 2, 3, 4, 5]; $evens = array_filter($numbers, function($num) { return $num [1] 2 == 0; }); print_r($evens); ?>
The modulus operator '%' returns the remainder. Checking if $num % 2 == 0 finds even numbers.
Fill both blanks to create an array of squares for numbers greater than 3 using array_map and array_filter with closures.
<?php $numbers = [1, 2, 3, 4, 5]; $filtered = array_filter($numbers, function($num) { return $num [1] 3; }); $squares = array_map(function($num) { return $num [2] $num; }, $filtered); print_r($squares); ?>
The first closure filters numbers greater than 3 using '>'. The second closure squares each number by multiplying it by itself using '*'.
Fill all three blanks to create an associative array with uppercase keys and values filtered by length greater than 4 using closures.
<?php $words = ['apple', 'bat', 'carrot', 'dog']; $result = array_filter(array_combine(array_map(function($word) { return [1]; }, $words), $words), function($word) { return strlen($word) [2] 4; }); $result = array_map(function($word) { return [3]; }, $result); print_r($result); ?>
The first blank converts each word to uppercase for keys using strtoupper($word). The second blank filters words with length greater than 4 using '>'. The third blank converts values to uppercase using strtoupper($word).