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

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


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.


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..));