As a PHP programmer, I use arrays for pretty much everything. I know SPLFixedArray can be useful in certain cases, and I know PHP arrays aren't very memory efficient, but I have rarely run into actual cases where they struggle to do what I need.
This in contrast to when I'm working in Java, where I find it absolutely critical that I understand exactly what data structures I'm using, and the pros and cons of each. If someone suggested I just use a LinkedHashMap for everything in Java, they'd be laughed out of the building.
So how can we get away with such fast and loose engineering in PHP? What are the underlying specifics of PHP arrays? It's often described as "an ordered map", but that leaves a lot of the implementation left to speculation.
What are some use cases PHP arrays are particularly good at? What are some seemingly straight forward use cases that PHP arrays are actually quite bad at?
For instance, I assume there's some sort of better handling of dense integer-keyed arrays (e.g. $arr = array('a','b','c','d','e');
) than an ordered hash map, but then where's the boundary between dense and sparse? Do arrays become dramatically less efficient as soon as I introduce even one non-ordered key, like $arr[10] = 'f';
? What about $arr[1000000] = 'g';
? I assume PHP doesn't populate the ~1 million slots inbetween, but if it's a linked list under the covers, then presumably calling $arr[rand()] = rand();
repeatedly would have to do some reordering after each insert?
Any answer that explores the underlying specifics of PHP arrays is welcome, even if it doesn't address the specific questions I raise.