PHP Array Question

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 :


$comment_array = get_approved_comments($post->ID) ;

foreach($comment_array as $comment){
      $comment_text = $comment->comment_content ;
   }

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):

$comment_text = $comment_array[ $last_comment_number]->comment_content ;

but I am confused as to the syntax.

Thanks for any help.

Second time something like this has come up lately… and literally ALL you need is:

$last_comment_text=end($comment_array)->comment_content;

PHP: end - Manual

I suggest learning all the internal pointer functions for arrays:
PHP: reset - Manual
PHP: end - Manual
PHP: current - Manual
PHP: each - Manual
PHP: prev - Manual
PHP: next - Manual

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.

Thanks ds60 - works perfectly. Also thanks for the links; I will have a read. As a C++ programmer, PHP arrays are still a bit of a mystery to me :smiley:

p.s. I thought I’d put the code in, in case anyone is ever searching for this solution:


$comment_array = get_approved_comments($post->ID) ;
$last_comment=end($comment_array);
$comment_text = $last_comment->comment_content ;

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

I have another solution for this problem.
If $comment_array is a onedimensional array,

$size=count($comment_array);
$last_comment_number=$size-1;
$comment_text = $comment_array[ $last_comment_number]->comment_content ;

This requires neither the looping statements nor the decision making statements

… 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.

PHP arrays should be thought of as FIFO queues. Add an item, and it goes on the end of the queue, regardless of it’s key.

Not quite. If the key matches an existing key, that existing key will be overwritten.

… and the ability to rip them clear out of the middle or add them in the middle.

see PHP: array_splice - Manual

and the behavior of PHP: unset - Manual


$test=array('a','b','c','d');
unset($test[1]);
print_r($test);

Will output [0]=‘a’, [2]=‘c’, [3]=‘d’ … while doing a count($test) will tell you three elements…

Which is why trusting numeric indexes isn’t always a great answer. Again, more like a pointered list/complex stream.

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).

Just curious,
Alex

What does stdio have to do with streams?

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.

Don’t forget you can use this to access the key in a foreach loop:

foreach($commets as $key => $value)
{

}