Optimal way to generate list of PHP object properties with delimiter character, implode()?

Posted by Kris on Stack Overflow See other posts from Stack Overflow or by Kris
Published on 2010-03-28T01:59:59Z Indexed on 2010/03/28 2:03 UTC
Read the original article Hit count: 285

Filed under:
|
|
|
|

I am trying to find out if there is a more optimal way for creating a list of an object's sub object's properties. (Apologies for the crude wording, I am not really much of an OO expert)

I have an object "event" that has a collection of "artists", each artist having an "artist_name". On my HTML output, I want a plain list of artist names delimited by a comma.

PHP's implode() seems to be the best way to create a comma delimited list of values. I am currently iterating through the object and push values in a temporary array "artistlist" so I can use implode().

That is the shortest I could come up with. Is there a way to do this more elegant?

$artistlist = array();
foreach ($event->artists as $artist)
{
    $artistlist[] = $artist->artist_name;
}
echo implode(', ', $artistlist);

© Stack Overflow or respective owner

Related posts about php

Related posts about object