Optimal way to generate list of PHP object properties with delimiter character, implode()?
- by Kris
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);