Array chunk and pad
📖 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 3Use
array_chunk with the $preserve_keys parameter set to false to split guests into chunks of size tableSizeUse
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