📖 Scenario: You are organizing a list of guests for a dinner party. You want to split the guests into tables with a fixed number of seats. If a table has fewer guests than seats, you want to fill the empty seats with the word "Empty".
🎯 Goal: Create a PHP program that splits an array of guest names into smaller arrays (tables) of a fixed size. If a table has fewer guests than the fixed size, fill the remaining seats with "Empty".
📋 What You'll Learn
Create an array called guests with exactly these names: 'Anna', 'Ben', 'Cara', 'David', 'Eva', 'Frank', 'Grace'
Create a variable called tableSize and set it to 3
Use array_chunk with the $preserve_keys parameter set to false to split guests into chunks of size tableSize
Use array_pad to fill any chunk smaller than tableSize with the string 'Empty'
Print the final array of tables
💡 Why This Matters
🌍 Real World
Organizing guests into tables for events or dinners where each table has a fixed number of seats.
💼 Career
Helps in understanding how to manipulate arrays for grouping data and filling missing values, useful in data processing and UI display tasks.
Progress0 / 4 steps
1
Create the guests array
Create an array called guests with these exact names: 'Anna', 'Ben', 'Cara', 'David', 'Eva', 'Frank', 'Grace'.
PHP
Hint
Use square brackets [] to create the array and separate names with commas.
2
Set the table size
Create a variable called tableSize and set it to 3.
PHP
Hint
Use the = sign to assign the value 3 to $tableSize.
3
Split guests into tables and pad
Use array_chunk with the $preserve_keys parameter set to false to split $guests into chunks of size $tableSize. Then, use a foreach loop with variables $index and $table to iterate over the chunks. Inside the loop, use array_pad to fill any chunk smaller than $tableSize with the string 'Empty'. Store the padded tables in a new array called paddedTables.
PHP
Hint
Use array_chunk to split the array, then loop over each chunk and use array_pad to add 'Empty' if needed.
4
Print the padded tables
Print the array paddedTables using print_r to display the final tables with padding.
PHP
Hint
Use print_r($paddedTables); to show the array structure.
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
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.
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.
Final Answer:
Splits an array into smaller arrays of a fixed size -> Option A
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
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.
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.
C. array_pad requires the size to be smaller than original
D. Missing the padding value argument in array_pad
Solution
Step 1: Check array_pad function signature
array_pad requires three arguments: the array, the new size, and the value to pad with.
Step 2: Identify missing argument
The code calls array_pad with only two arguments, missing the padding value, which causes an error.
Final Answer:
Missing the padding value argument in array_pad -> Option D
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
Step 1: Understand the goal
We want chunks of size 2, each chunk exactly length 2, padding with 0 if chunk is smaller.
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.
Final Answer:
$chunks = array_chunk($data, 2);
foreach ($chunks as &$chunk) {
$chunk = array_pad($chunk, 2, 0);
}
print_r($chunks); -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.