Playground Checkpoint
Turn a flat list of bookings into a per-barber, time-sorted day view — the expected output tells you when it's right.
The Challenge
Olivia wants the counter screen ready before Leo's first day on Monday. The input is the raw list of the day's bookings, in the order they were made, with a cancelled one among them. The screen needs each barber by name and ID, in ID order, with their confirmed bookings in time order, and the day's total takings at the bottom.
Open the starter in the PHP Playground with the button below. It has Mia's first attempt: the barbers joined with array_merge(), a filter followed by a for loop, a sort inside a foreach, and a total over every booking. Run it: warnings, missing bookings, a wrong order and a wrong total. Fix it with what you learned in this Act.
What You Need to Do
- Run the starter and list every problem in the output. For each one, note which chapter explains it.
- Join
$teamand$newStartersso the barber IDs survive (chapter 6). - Rewrite
dayView(): keep the confirmed bookings, sort them by start time as times, not as text (chapter 8), and group them by barber ID (chapter 9), with the barbers in ID order. - Don't loop over the filtered list by position, and don't sort a copy inside a
foreach(chapters 3, 4 and 5). - Make
dayTotal()count only the confirmed bookings. - Don't change the bookings or the lines under "Print the day view". The output must match the expected output exactly, with no warnings.
Stuck? A Few Hints
- array_merge() renumbers integer keys; + and array_replace() keep them (chapter 6).
- array_filter() keeps the keys, so after filtering there's a gap where Ann's cancelled booking was. Use foreach, not for (chapter 5).
- "10:00" <=> "9:30" compares text, so 10:00 comes first. strnatcmp() compares the numbers inside (chapter 8).
- Sort the whole list first, then group with $day[$booking["barber"]][] = $booking; and finish with ksort($day) (chapter 9).
Ready to Build It?
Opens the Playground, right in your browser — nothing to install.
A correct run prints nine lines: "Ben (barber 12): 3 bookings" with 9:00, 10:00 and 11:00, then "Leo (barber 15): 3 bookings" with 9:30, 10:00 and 11:30, and "Day total: £131.00" — no warnings, and no Ann Lee. The starter prints "Undefined array key" warnings, barbers without names, an extra empty group, Sam Ortiz missing, Ben's times unsorted and a total of £156.00.
Before You Move On
If your output matches, Tom's counter screen shows every barber's day in order, with the right total — and Leo's first day can start. Along the way you've used most of PHP's array toolbox: keys and their rules, adding and removing, destructuring and references, filter, map and reduce, merging without losing IDs, set operations, sorting by meaning, slicing and grouping, and the newest helpers. Next: functions — and one price calculation copied into six files.
