First of all sorry for the long winded explanation to a simple question about array syntax in PHP. I am having a small problem with a bit of code I have added to my wordpress blog to display the last comment on a particular post. I am using the following code to get the last comment into $comment_text :
That works fine, but I can’t work out how to just get the last comment from the $comment_array without looping through all the comments - something like this (where $last_comment_number is the value of the last entry):
They’re REALLY handy once you understand what the “internal pointer” is and how it works, as you can use it to traverse an array selectively.
like say… foreach in reverse?
if ($record=end($myarray)) do {
/* something with $record */
} while ($record=prev($myarray);
Some of the most powerful functions in PHP, and sadly some of the least used and barely known even by people who claim to be PHP ‘experts’. Basically these functions let you treat an array as if it was what is known as a ‘file of record’ (or struct) in heavier duty programming languages like C or Pascal.
Coming from a machine language (as in hand assembling), ASM, Pascal and C background – PHP’s lack of typecasting often has me similarly stymied.
In terms of arrays, once I realized that PHP arrays are NOT in fact stored as a normal programming language array, and are what we would call a “stream” – it began to make a whole lot more sense. The “key” just being another arbitrary value in the stream instead of an actual index for storing the file.
We in fact do not have access to the REAL key used internally for array pointers except via those commands I linked to… I suspect that there isn’t actually a true array index nor are they actually arrays – they’re a string of linked objects.
In pascal parliance:
type
pArrayRecord=^arrayRecord;
arrayRecord=object
prev,next:pArrayRecord;
data:pPhpVariable;
end;
arrayList=object
first,
last,
current:pArrayRecord;
end;
ArrayList’s constructor would make first, last and current all null until you add a record. An “add to end” function would simply do if last is null, make the new one as last, copying the pointer to first and current and setting it’s prev and next to null… if it’s not null, you’d make last^.next equal the new one, then point last at it.
I’ve been playing with the idea of making my own php-like interpreter just to get some insights into how it works under the hood… you know, for fun…
… and only works if the keys are numeric and in sequence, something you can’t always rely on being true as php arrays aren’t arranged how other languages handle arrays.
In terms of arrays, once I realized that PHP arrays are NOT in fact stored as a normal programming language array, and are what we would call a “stream” – it began to make a whole lot more sense. The “key” just being another arbitrary value in the stream instead of an actual index for storing the file.
How are arrays anything like streams? To my knowledge they provide no stdio, that is accomplished through a variety of methods depending on the environment (CLI/HTTP).
I think we’re thinking two different types of “streams” or something… Streams - a linked list of objects that you can pass to any source and navigate like it was a record/structure based file.