Recall & Review
beginner
What does the PHP function
array_chunk() do?It splits an array into smaller arrays (chunks) of a specified size. Each chunk is a new array containing elements from the original array.
Click to reveal answer
beginner
How does
array_pad() work in PHP?It adds elements to an array until it reaches a specified size. You can add elements at the end (positive size) or at the beginning (negative size).
Click to reveal answer
intermediate
What happens if the size parameter in
array_chunk() is larger than the array length?The function returns one chunk containing the entire original array because it cannot split into smaller chunks.
Click to reveal answer
intermediate
Can
array_pad() shrink an array if the size is smaller than the current array length?No,
array_pad() only adds elements if the array is smaller than the specified size. It does not remove elements.Click to reveal answer
beginner
Show a simple example of using
array_chunk() and array_pad() together.Example:<br>
$arr = [1, 2, 3, 4]; $chunks = array_chunk($arr, 2); // [[1, 2], [3, 4]] $padded = array_pad($chunks[0], 4, 0); // [1, 2, 0, 0]
Click to reveal answer
What does
array_chunk([1,2,3,4,5], 2) return?✗ Incorrect
The array is split into chunks of size 2. The last chunk contains the remaining element.
What will
array_pad([1,2], 5, 0) return?✗ Incorrect
The array is padded at the end with zeros until it reaches size 5.
If you use
array_pad([1,2,3], -5, 'x'), where are the elements added?✗ Incorrect
A negative size pads the array at the beginning.
What happens if you call
array_chunk([1,2,3], 10)?✗ Incorrect
Since chunk size is larger than array length, the whole array is one chunk.
Does
array_pad() remove elements if the array is bigger than the size?✗ Incorrect
array_pad only adds elements; it never removes them.
Explain how you can split an array into smaller parts and then make sure each part has the same length by adding extra elements.
Think about splitting first, then adding elements to chunks.
You got /3 concepts.
Describe the difference between positive and negative size values in array_pad.
Where do new elements go depending on sign of size?
You got /3 concepts.