Bird
Raised Fist0
PHPprogramming~20 mins

Array chunk and pad in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of array_chunk with padding
What is the output of the following PHP code?
PHP
<?php
$array = [1, 2, 3, 4, 5];
$chunks = array_chunk($array, 2, true);
print_r($chunks);
?>
AArray ( [0] => Array ( [0] => 1 [1] => 2 ) [1] => Array ( [2] => 3 [3] => 4 ) )
BArray ( [0] => Array ( [0] => 1 [1] => 2 ) [1] => Array ( [0] => 3 [1] => 4 ) [2] => Array ( [0] => 5 ) )
CArray ( [0] => Array ( [0] => 1 [1] => 2 ) [1] => Array ( [2] => 3 [3] => 4 ) [2] => Array ( [0] => 5 ) )
DArray ( [0] => Array ( [0] => 1 [1] => 2 ) [1] => Array ( [2] => 3 [3] => 4 ) [2] => Array ( [4] => 5 ) )
Attempts:
2 left
💡 Hint
Remember that the third parameter true preserves keys in array_chunk.
❓ Predict Output
intermediate
2:00remaining
Output of array_pad with negative size
What is the output of this PHP code snippet?
PHP
<?php
$array = [10, 20];
$padded = array_pad($array, -5, 0);
print_r($padded);
?>
AArray ( [0] => 0 [1] => 0 [2] => 0 [3] => 10 [4] => 20 )
BFatal error: array_pad(): Size must be greater than zero
CArray ( [0] => 10 [1] => 20 )
DArray ( [0] => 10 [1] => 20 [2] => 0 [3] => 0 [4] => 0 )
Attempts:
2 left
💡 Hint
Negative size pads at the beginning of the array.
🔧 Debug
advanced
2:00remaining
Identify the error in array_chunk usage
What error does this PHP code produce?
PHP
<?php
$array = [1, 2, 3, 4];
$chunks = array_chunk($array, 0);
print_r($chunks);
?>
AArray ( )
BFatal error: array_chunk() expects parameter 2 to be integer, string given
CWarning: array_chunk(): Size parameter expected to be greater than 0
DNo output, script runs silently
Attempts:
2 left
💡 Hint
Check the minimum allowed value for the size parameter in array_chunk.
❓ Predict Output
advanced
2:00remaining
Result length after array_pad
How many elements does the array have after this code runs?
PHP
<?php
$array = [5, 6, 7];
$padded = array_pad($array, 6, 9);
echo count($padded);
?>
A6
B9
C3
DError
Attempts:
2 left
💡 Hint
array_pad extends the array to the specified size if needed.
🧠 Conceptual
expert
3:00remaining
Understanding array_chunk with preserve_keys and array_pad combined
Given the code below, what is the output?
PHP
<?php
$array = [0 => 'a', 1 => 'b', 2 => 'c'];
$chunks = array_chunk($array, 2, true);
$chunks[1] = array_pad($chunks[1], 3, 'x');
print_r($chunks);
?>
AArray ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [0] => x [1] => x ) )
BArray ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => x [4] => x ) )
CArray ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => x ) )
DArray ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c ) )
Attempts:
2 left
💡 Hint
array_chunk with preserve_keys keeps original keys; array_pad adds elements with numeric keys starting from 0.

Practice

(1/5)
1. What does the PHP function array_chunk do?
easy
A. Splits an array into smaller arrays of a fixed size
B. Adds elements to the end of an array to reach a certain size
C. Removes duplicate values from an array
D. Sorts an array in ascending order

Solution

  1. Step 1: Understand the function purpose

    array_chunk is designed to split a large array into smaller arrays each having a fixed number of elements.
  2. Step 2: Compare with other options

    Adding elements to reach a size is done by array_pad, not array_chunk. Removing duplicates or sorting are unrelated functions.
  3. Final Answer:

    Splits an array into smaller arrays of a fixed size -> Option A
  4. Quick Check:

    array_chunk = split array [OK]
Hint: Remember: chunk means break into pieces [OK]
Common Mistakes:
  • Confusing array_chunk with array_pad
  • Thinking it sorts or removes duplicates
  • Assuming it modifies the original array in place
2. Which of the following is the correct syntax to pad an array $arr to size 5 with the value 0 using array_pad?
easy
A. $padded = array_pad($arr, 5);
B. $padded = array_pad(5, $arr, 0);
C. $padded = array_pad($arr, 5, 0);
D. $padded = array_pad($arr, 0, 5);

Solution

  1. Step 1: Recall array_pad syntax

    The correct syntax is array_pad(array, size, value), where the first argument is the array, second is the desired size, third is the padding value.
  2. Step 2: Check each option

    $padded = array_pad($arr, 5, 0); matches the correct order and arguments. Options B and C have wrong argument order. $padded = array_pad($arr, 5); misses the padding value.
  3. Final Answer:

    $padded = array_pad($arr, 5, 0); -> Option C
  4. Quick Check:

    array_pad(array, size, value) correct order [OK]
Hint: Remember: array_pad(array, size, value) [OK]
Common Mistakes:
  • Swapping argument order
  • Omitting the padding value
  • Passing size as first argument
3. What is the output of the following PHP code?
$arr = [1, 2, 3, 4, 5];
$chunks = array_chunk($arr, 2);
print_r($chunks);
medium
A. [[1, 2, 3], [4, 5]]
B. [[1, 2], [3, 4], [5]]
C. [[1, 2], [3, 4], [5, null]]
D. [1, 2, 3, 4, 5]

Solution

  1. Step 1: Understand array_chunk behavior

    array_chunk splits the array into smaller arrays each with 2 elements, except the last chunk which may have fewer if elements run out.
  2. Step 2: Apply to given array

    Splitting [1,2,3,4,5] by 2 gives chunks: [1,2], [3,4], and [5]. No padding is added by array_chunk.
  3. Final Answer:

    [[1, 2], [3, 4], [5]] -> Option B
  4. Quick Check:

    array_chunk splits into groups of 2 [OK]
Hint: Last chunk may be smaller if not divisible [OK]
Common Mistakes:
  • Expecting padding in array_chunk
  • Confusing chunk size with number of chunks
  • Assuming all chunks have equal length always
4. Identify the error in this PHP code snippet:
$arr = [1, 2];
$padded = array_pad($arr, 5);
print_r($padded);
medium
A. print_r cannot print arrays
B. array_pad cannot increase array size
C. array_pad requires the size to be smaller than original
D. Missing the padding value argument in array_pad

Solution

  1. Step 1: Check array_pad function signature

    array_pad requires three arguments: the array, the new size, and the value to pad with.
  2. Step 2: Identify missing argument

    The code calls array_pad with only two arguments, missing the padding value, which causes an error.
  3. Final Answer:

    Missing the padding value argument in array_pad -> Option D
  4. Quick Check:

    array_pad needs 3 arguments [OK]
Hint: array_pad needs array, size, and value [OK]
Common Mistakes:
  • Forgetting the padding value
  • Assuming array_pad works with two arguments
  • Thinking print_r can't print arrays
5. You have an array $data = [10, 20, 30]. You want to split it into chunks of size 2 and ensure each chunk has exactly 2 elements by padding with 0 if needed. Which code correctly achieves this?
hard
A. $chunks = array_chunk($data, 2); foreach ($chunks as &$chunk) { $chunk = array_pad($chunk, 2, 0); } print_r($chunks);
B. $chunks = array_pad(array_chunk($data, 2), 2, 0); print_r($chunks);
C. $chunks = array_chunk(array_pad($data, 5, 0), 2); print_r($chunks);
D. $chunks = array_chunk($data, 2, 0); print_r($chunks);

Solution

  1. Step 1: Understand the goal

    We want chunks of size 2, each chunk exactly length 2, padding with 0 if chunk is smaller.
  2. Step 2: Analyze each option

    $chunks = array_chunk($data, 2); foreach ($chunks as &$chunk) { $chunk = array_pad($chunk, 2, 0); } print_r($chunks); splits into chunks, then pads each chunk to size 2 with 0, which is correct.
    $chunks = array_pad(array_chunk($data, 2), 2, 0); print_r($chunks); tries to pad the array of chunks, which is incorrect.
    $chunks = array_chunk(array_pad($data, 5, 0), 2); print_r($chunks); pads the original array to size 5, then chunks, which may add unwanted extra elements.
    $chunks = array_chunk($data, 2, 0); print_r($chunks); passes 0 as third argument to array_chunk, which is invalid.
  3. Final Answer:

    $chunks = array_chunk($data, 2); foreach ($chunks as &$chunk) { $chunk = array_pad($chunk, 2, 0); } print_r($chunks); -> Option A
  4. Quick Check:

    Pad each chunk after chunking [OK]
Hint: Pad each chunk after chunking, not the whole array [OK]
Common Mistakes:
  • Padding the whole array before chunking
  • Passing extra arguments to array_chunk
  • Trying to pad the array of chunks directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard