Member-only story
You’re Using an Array for That? 10 PHP Data Structures You’re Ignoring
PHP’s array does everything — including jobs a real stack, queue, or heap does better. 10 built-in data structures you’re ignoring.
PHP’s array is a marvel and a crutch. It’s an ordered map, a list, a stack, a queue, a set, and a lookup table all at once. That’s wonderful — right up until it’s the reason your code is harder to read and slower than it needs to be. Because the array does everything passably, most PHP developers reach for it to do everything. That includes jobs where a purpose-built data structure would be clearer about intent, safer against misuse, and occasionally much faster.
PHP ships an entire library of those purpose-built structures, bundled in the Standard PHP Library, and most people have never touched them. A real stack that can’t be accidentally indexed into. A queue that reads like a queue. A priority queue that always hands you the most urgent item. A structure that lets you use objects themselves as keys. They’ve been in the language the whole time, requiring no installation, and they turn a certain kind of “clever array trick” into plain, self-documenting code.
This is the tour: ten data structures from the SPL, what each is actually for, the code to use it, and when it genuinely beats reaching for an array again. Everything below was run on PHP…




