0
0
PHPprogramming~5 mins

Array chunk and pad in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A[[1,2], [3,4], [5]]
B[[1,2,3], [4,5]]
C[[1,2], [3,4,5]]
D[[1], [2], [3], [4], [5]]
What will array_pad([1,2], 5, 0) return?
A[1, 2, 5]
B[1, 2]
C[0, 0, 1, 2]
D[1, 2, 0, 0, 0]
If you use array_pad([1,2,3], -5, 'x'), where are the elements added?
AAt the end
BAt the beginning
CNo elements are added
DElements replace existing ones
What happens if you call array_chunk([1,2,3], 10)?
A[[1,2,3]]
B[]
C[[1,2], [3]]
DError
Does array_pad() remove elements if the array is bigger than the size?
AYes, it removes extra elements
BIt throws an error
CNo, it only adds elements
DIt duplicates elements
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.