0
0
PHPprogramming~20 mins

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

Choose your learning style9 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.