dimo414
dimo414

Reputation: 48864

What are the characteristics of PHP's array type as a data structure?

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.

Upvotes: 9

Views: 885

Answers (2)

dirtside
dirtside

Reputation: 8280

The fundamental problem with PHP arrays is that they're a mash-up of two different data types: arrays and maps. Arrays a la Javascript or Python are simple, ordered lists, indexed numerically starting at 0. Very easy to understand and use. Maps (aka dictionaries) are (usually unordered) collections of key-value pairs. Again, very simple to understand and use.

PHP arrays can be both, and act like both, depending on what you do with them, and certain operations using PHP's array functions can cause them to behave in unexpected ways. Array keys can be (for example) strings or integers, but you cannot have a string key that is numeric, for PHP will forcibly convert it to an integer no matter what you do. This can create problems when (for example) converting data to and from JSON, because you can end up with multiple similar numeric keys of different types.

PHP's developers should have kept the two data types distinct. It might be convenient to use array notation to make an on-the-fly map, but they shouldn't have done it. I'm not a huge fan of Python (...yet) but the formal distinction between lists and maps is one thing they certainly have done better than PHP.

Upvotes: 1

user1086498
user1086498

Reputation:

PHP arrays are amazing at modeling arbitrary space coordinates. You pretty easily create a cache of perlin noise values - positive, negative, and so forth.

PHP arrays are great for representing configuration objects. Flexible key types make this a snap.

PHP arrays confuse you about the difference between a key and an index. Very badly.

PHP arrays are just generally slower - though it could be PHP itself and not really the arrays - and always give you more options than you really ever need. It leads to horrible questions like this one:

PHP: Best way to iterate two parallel arrays?

Just look at his arrays. They're... what are they? Arbitrary parameter lists?

Also another thing php arrays are great at!

$class->call('func', Array(..params..));

Upvotes: 1

Related Questions