Complete the code to split the array into chunks of size 2.
<?php $numbers = [1, 2, 3, 4, 5]; $chunks = array_chunk($numbers, [1]); print_r($chunks); ?>
The array_chunk function splits the array into smaller arrays of the given size. Here, size 2 means each chunk has 2 elements.
Complete the code to pad the last chunk to size 3 with zeros.
<?php $numbers = [1, 2, 3, 4, 5]; $chunks = array_chunk($numbers, 3); $last_chunk = $chunks[array_key_last($chunks)]; $padded = array_pad($last_chunk, [1], 0); print_r($padded); ?>
The array_pad function pads the array to the specified length. Here, padding to 3 adds zeros if the last chunk has fewer than 3 elements.
Fix the error in the code to pad the array to length 6 with -1.
<?php $arr = [1, 2, 3]; $padded = array_pad($arr, [1], -1); print_r($padded); ?>
To pad the array to length 6, the second argument of array_pad must be 6.
Fill both blanks to create chunks of size 2 and pad each chunk to size 3 with 0.
<?php $numbers = [1, 2, 3, 4, 5]; $chunks = array_chunk($numbers, [1]); foreach ($chunks as &$chunk) { $chunk = array_pad($chunk, [2], 0); } print_r($chunks); ?>
Chunks are created with size 2, then each chunk is padded to size 3 with zeros.
Fill all three blanks to chunk the array by 3, pad each chunk to 4 with -1, and print the result.
<?php $values = [10, 20, 30, 40, 50, 60, 70]; $chunks = array_chunk($values, [1]); foreach ($chunks as &$chunk) { $chunk = array_pad($chunk, [2], [3]); } print_r($chunks); ?>
The array is split into chunks of 3 elements, then each chunk is padded to length 4 with -1.