Praise for PHP Arrays

I have to give credit to the PHP team for creating what is one of the most flexible, easy to use, and powerful data structures I have used. That is the array.

To be completely accurate, the manual describes it as an ordered map,  but it can be used for almost any purpose.

As I’ve worked with PHP, you realize that almost everything is stored and manipulated as an array. PHP globals, Objects, methods, even constants can be stored as an array. And they have optimized this data type so it is fast.

HTML tags are perfectly suited for arrays, as it is an ordered map of name value pairs:

<a href=”…” target=”_blank”>…</a>

array(“href” => “…”, “target” => “_blank”)

As is XML, form data (name value pairs), and pretty much most web technologies. (Think HTTP header and response: Ordered name-value pairs.)

I have become so attached to the Array that when I learn new languages I try to find the equivalent structure to use.

For Visual Basic and ASP, for example, there is a Scripting.Dictionary object which, with some quick wrapper functions, can be made to behave similarly to Arrays.

Java, however, doesn’t apper to have any “built-in” array-like functionality, and as such I had to tap the Apache Commons OrderedMap object to get something similar.

Obviously, Perl developers will probably invoke the hash as the original inspiration for the array, but because of the lack of internal ordering and the “Array” and “Hash” duplicity, I would disagree.

JavaScript has Objects, which are a close second to PHP arrays, without the supporting built-in  functions.

Ruby has Hashes and Lists, similar to Perl, and Python has objects which represent sets, lists, etc., akin to Java which has a data type depending on the mood you are in.

However, no other language has come close to the flexibility and ease of use of PHP arrays. I think what makes them so powerful is that they are packed with functionality, and behave the way you want to use them.

One reply on “Praise for PHP Arrays”

Comments are closed.