In this chapter
We'll learn how PHP arrays work underneath — a hash table that finds a key in one step — why isset() on a key is fast and in_array() on a long list is slow, how copy-on-write keeps array copies cheap, and when a record should become an object or a collection instead of an array.
The Problem in Real Life
Olivia wants a month view: 30 days, three barbers, 18 slots a day — every slot marked free or booked. Mia builds it with three nested loops, and for every slot she asks in_array($slot, $bookedSlots), where $bookedSlots is the list of the month's 1,200 bookings. The page takes eleven seconds to load.
Worse, one slot shows as free although it's booked. In one place she typed $booking["strat"] instead of $booking["start"]. PHP printed a warning into a log nobody reads, used null, and carried on.
It's only 1,620 slots. Why is it so slow — and why didn't PHP stop at my typo?
Mia
Searching Values vs. Looking Up Keys
Searching is slow
in_array() checks items one by one. 1,620 slots, each checked against up to 1,200 bookings, is over a million checks.
Keys are fast
Looking up a key takes one step, however big the array is.
Arrays don't check names
A typo in an array key is just another key that doesn't exist. Objects can refuse it.
Array Performance: Arrays vs Objects vs Collections
What's underneath: a PHP array is a hash table: PHP turns each key into a number that says where the value is stored, so it can jump straight to it. That one fact explains most of this chapter:
- Speed: looking up a key is one step; searching for a value means checking items one by one.
- Memory: copies are cheap until you change them (copy-on-write).
- Arrays vs. objects: objects give records fixed, checked field names (Act 11).
- Arrays vs. collections: a collection is an object that wraps an array with chainable methods, like Laravel's.
Keys are found in one step: isset($booked["9:30"]) and $prices["Haircut"] take the same short time whether the array has 10 items or 10 million, because PHP computes where the key lives. in_array(), array_search() and array_find() have no such shortcut: they check one item after another until they find a match or reach the end. One search is still fast. But Mia's month view did it 1,620 times, over up to 1,200 items each — more than a million comparisons. The fix is to turn the booked list into keys once, with array_flip() (chapter 7) or by building it keyed from the start, and then ask isset() 1,620 times. Rule of thumb: if you search the same list more than once, key it.
<?php$bookedSlots = ["2026-11-02 9:30", "2026-11-02 11:00", "2026-11-03 9:00"]; // in reality, 1,200// Slow at scale: every check walks the whole listvar_dump(in_array("2026-11-02 11:00", $bookedSlots, true)); // bool(true)// Fast at any size: flip once, then every check is one step$isBooked = array_flip($bookedSlots);var_dump(isset($isBooked["2026-11-02 11:00"])); // bool(true)var_dump(isset($isBooked["2026-11-02 10:00"])); // bool(false)
<?php$booked = range(1, 1200); // 1,200 booked slot numbers$slots = range(1, 1620); // 1,620 slots in the month view$checks = 0;foreach ($slots as $slot) {foreach ($booked as $b) { // what in_array() does inside$checks++;if ($b === $slot) {break;}}}echo number_format($checks), " comparisons with in_array()\n"; // 1,224,600 comparisons with in_array()$isBooked = array_flip($booked);echo number_format(count($slots)), " lookups with isset()\n"; // 1,620 lookups with isset()
Copies are cheap until you change them: assigning an array, $copy = $day;, or passing it to a function doesn't copy anything at first. Both variables share the same data until one of them changes it; only then does PHP make a real copy. This is called copy-on-write. So passing big arrays to functions is cheap, and you don't need references (&) for speed — use them only when a function should change the caller's array. What does cost memory is keeping huge arrays around: 100,000 bookings as arrays of arrays take many megabytes. When data gets that large, process it in pieces (chapter 9) or let the database do the work (Act 18).
<?php$day = ["9:00", "9:30", "10:00"];$copy = $day; // no real copy yet: both share the data$copy[] = "10:30"; // now PHP copies, then changes only $copyecho count($day), " ", count($copy), "\n"; // 3 4function countSlots(array $slots): int // passing an array is cheap too{return count($slots);}echo countSlots($day), "\n"; // 3
Arrays vs. objects: an array is a bag of keys. Nothing stops you from writing $booking["strat"]; PHP warns and carries on with null, and that's how a booked slot became free. An object of a class (Act 11) declares its fields, called properties, with types: $booking->start must exist and be, say, an int. A typo like $booking->strat is an error you can't miss, and your editor underlines it before you even run the code. Use arrays for lists and for quick, local data; once a record travels through your program — a booking, a customer, a barber — make it an object. Act 11 starts exactly there.
<?php$booking = ["start" => 570, "customer" => "Tom Hart"];$isBooked = $booking["strat"] !== null; // typo: "strat"var_dump($isBooked);// Warning: Undefined array key "strat"// bool(false) — the slot looks free
Arrays vs. collections: a collection is an object that holds an array and gives you chainable methods: collect($day)->filter(...)->sortBy("start")->groupBy("barber"). It's the map, filter and sort of this Act in one readable line. PHP itself has no general collection class; frameworks bring their own, and the one you'll use is Laravel's Collection in the next course. Underneath, it's still a PHP array — so everything in this Act, including the cost of searching values, still applies. The table below compares the three.
| Question | Array | Object (Act 11) | Collection (Laravel) |
|---|---|---|---|
| Field names checked? | No: a typo gives a warning and null | Yes: an error, and the editor sees it | No: it holds arrays or objects |
| Types checked? | No | Yes, with typed properties | Only what its items check |
| Good for | Lists, lookups, quick local data | Records: bookings, customers | Chaining filter, sort, group |
| Built into PHP? | Yes | Yes | No: comes with the framework |
Key Takeaway
A PHP array is a hash table: looking up a key (isset, $a[$k]) takes one step at any size, while searching values (in_array, array_search) checks item by item — so key a list once if you look things up in it many times. Copies are cheap until changed (copy-on-write). Use arrays for lists and local data; use objects for records with fixed, checked fields; collections wrap arrays with chainable methods.
Why This Matters
A page that takes eleven seconds loses customers before they book. Most slow PHP pages aren't slow because PHP is slow; they search the same list again and again. Knowing that keys are instant and searches aren't is the most useful performance rule in this whole course.
And knowing where arrays stop being the right tool — records whose fields must exist — leads straight into the next big part of the course: objects.
The month view keys the booked slots once, and loads in a blink. The typo is fixed, and Mia has a note on her screen: "Records → objects, Act 11." One job is left for this Act: the day view itself, built from the raw list of bookings with everything she has learned. That's the checkpoint.
