I'm tying to iterate through an array, assembling a string to return each time. 

My question is how can I omit the comma on the last iteration of the array, or if there is only one element to the array? I'm not sure what this operation would be called as my coding skills are very rudimentary, so I've not had much luck searching for an answer. Even help knowing this basic detail would be much appreciated. 

this is the result I'd like:

```
{ image : 'http://www.site.com/path/to/file/image1.jpg', title : 'Some title and caption' url : 'http://www.site.com/path/to/file/image1.jpg' },
{ image : 'http://www.site.com/path/to/file/image1.jpg', title : 'Some title and caption' url : 'http://www.site.com/path/to/file/image1.jpg' },
{ image : 'http://www.site.com/path/to/file/image1.jpg', title : 'Some title and caption' url : 'http://www.site.com/path/to/file/image1.jpg' }

```

Note the lack of a trailing comma.
Below is the php Im using to generate the strings. It will always include a trailing comma which is causing me all sorts of greif. 

```
//snipit
$i = 1;
$a = '';  
foreach ($pages as $go)
{
    $title  = ($go['media_title'] == '') ? '&nbsp;' : $go['media_title'];
    $caption = ($go['media_caption'] == '') ? '&nbsp;' : $go['media_caption'];

    $a .= "{ image :'" . BASEURL . GIMGS . "/$go[media_file]', title : '{$title}, {$caption}', url: '" . BASEURL . GIMGS . "/$go[media_file]' }";
    $a .= ",\n";

$i++; 
return $a;
}

```

Many thanks for your experience, 

orionrush