Complete the code to get the number of elements in the array.
<?php $fruits = ['apple', 'banana', 'cherry']; $count = [1]($fruits); echo $count; ?>
The count() function returns the number of elements in an array.
Complete the code to add a new element 'orange' to the end of the array.
<?php $fruits = ['apple', 'banana']; array_[1]($fruits, 'orange'); print_r($fruits); ?>
The array_push() function adds one or more elements to the end of an array.
Fix the error in the code to check if 'banana' is in the array.
<?php $fruits = ['apple', 'banana', 'cherry']; if ([1]('banana', $fruits)) { echo 'Found banana'; } else { echo 'Banana not found'; } ?>
The in_array() function checks if a value exists in an array and returns true or false.
Fill both blanks to create an array of squares for numbers 1 to 5.
<?php $numbers = [1, 2, 3, 4, 5]; $squares = array_map(function([1]) { return [2] * [1]; }, $numbers); print_r($squares); ?>
The anonymous function takes each number as $n and returns its square by multiplying $n * $n.
Fill all three blanks to filter numbers greater than 3 and create a new array with those numbers doubled.
<?php $nums = [1, 2, 3, 4, 5]; $filtered = array_filter($nums, function([1]) { return [2] > 3; }); $doubled = array_map(function([3]) { return [3] * 2; }, $filtered); print_r($doubled); ?>
The filter function uses $num to check if the number is greater than 3. The map function uses $n to double each filtered number.