Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 437
dimo414 Avatar asked Apr 13 '12 02:04

dimo414


People also ask

What are the 3 types of arrays?

There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.

What are the types of array in data structure?

Description: Array: collection of fixed number of components (elements), wherein all of components have same data type. One-dimensional array: array in which components are arranged in list form. Multi-dimensional array: array in which components are arranged in tabular form (not covered)

What are the different types of array in PHP explain with example?

In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

What is an PHP array?

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.


1 Answers

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.

like image 102
dirtside Avatar answered Oct 21 '22 01:10

dirtside