[thelist] Q on PHP array manipulations...

Ken Schaefer ken at adOpenStatic.com
Sun Jul 11 08:08:33 CDT 2004


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "sbeam" <sbeam at syxyz.net>
Subject: Re: [thelist] Q on PHP array manipulations...


: On Friday 09 July 2004 08:54 am, Ken Schaefer wrote:
: > : arrays don't have columns or rows. They have keys
: > : and values.
: >
: > Nit pick. I think you may be confusing associative arrays, like hash
: > tables, and binary search trees (which have have keys/values) with
: > arrays. That said, you are correct in asserting that arrays do not
: > have "rows and columns"
:
: uh, who's nit picking now? ;) Actually it's an important distinction,
: anyone talking about a "row" in an array is maybe confused about what
: is going on in general. Makes technical communication difficult...
:
: Also if you read the PHP docs you'll see that its all key=>value when it
: comes to arrays, even so-called 'linear' arrays which are really just
: associative arrays with consecutive integers for keys. PHP does not
: have regular arrays like most languages. This is one of its
: "interesting features" - read the docs for the each() function to taste
: the insanity http://us4.php.net/each
:
: or you could look at this, first 2 sentences:
: http://www.php.net/manual/en/language.types.array.php
: "An array in PHP is actually an ordered map. A map is a type that maps
: values to keys."
:
: I don't know or care what they do with arrays internally. It's not a
: regular hash since they are ordered.

Well, as I understand it, any linked-list is "ordered" internally (though
this may not be exposed publicly). In order for a linked list (and this is
how many associative arrays, if not all, are implemented) to work, each
"item" contains both the item's data and a pointer to the next item. This
allows for easy "dynamic" insertion of new elements. The element can be
inserted into the "middle" of the linked list, or it can be appended at the
"end". All that needs to happen is for a few pointers to be updated.
However, this usually makes lookups slow, since (in a worst case), every
item in the linked list needs to be traversed, in order to locate the
matching key, and it's associated value.

On the other hand, an array (in the classical sense) is contained in a
contiguous block of memory. This makes an array idea for data lookups (to
access an element you only need to know the size of the data type that the
array contains, plus the subscript, which indicates the amount to offset the
starting memory address that the array is contained within). However, this
also makes it an expensive operation to "redefine" an array, since another,
larger, block of memory must be located, and everything from the existing
array copied to the new memory location.

Well, that's how I remember (or is it "misremember"?) the difference between
associative arrays and arrays from my programming classes at Uni... :-)

Cheers
Ken



More information about the thelist mailing list