Main Content

When an array is not an array

Archive - Originally posted on "The Horse's Mouth" - 2008-04-17 06:40:22 - Graham Ellis

An array, I was taught, is a sequential series of memory locations in which values of some type are stored. In an array, each of the memory locations is the same size (number of bytes). And so, as I was taught, arrays can be used very efficiently using pointer arithmetic, BUT they need to:

a) Be defined in advance
b) Have each element given enough memory to allow for the biggest content

and

c) They cannot be lengthened later
d) Taking an element out in the middle calls for a lot of shuffling up.

Arrays in PHP and in Tcl are NOT arrays! The word "array" is used more loosely to define a collection object which is (in computer science terms) either a link list or a hash. And that means ...

i) You can define PHP or Tcl arrays as you need them
ii) Elements can have different sizes
iii) Arrays in Tcl of PHP can be expanded as need be
iv) Elements can be deleted easily from the middle
v) Elements can be named (PHP's "associative arrays, all Tcl arrays) rather than numbered

You gain all that flexibility ... what do you loose? For a few applications - the intense dataprocessing / calculation ones - your application may slow down a bit. Since PHP is a web server language where you aren't typically doing such calculations, that turns out to be no loss at all!