Anagio
Anagio

Reputation: 3075

Remove comma from end of this string?

When echoing $teststring I get the string below from the foreach loop. I needed to remove the trailing comma and tried echo rtrim($teststring,','); this removes commas from inbetween each bracketed set of data. How can I remove only the last comma?

Thanks

[Date.UTC(2013,15,6), 9 ],[Date.UTC(2013,15,7), 9 ],[Date.UTC(2013,15,8), 9 ],[Date.UTC(2013,15,9), 9 ],[Date.UTC(2013,15,10), 9 ],[Date.UTC(2013,15,11), 9 ],[Date.UTC(2013,15,12), 9 ],

I need to remove the trailing comma

foreach ($filtered_decoded as $results) {

   $date = str_replace("-",",",$results['date']);
   $pos = $results['position'];

    $arr = array("data"=>"[Date.UTC(".$date."), ".$pos." ],");

    // Tried to remove comma.
    $teststring = implode($arr);
    echo rtrim($teststring,',');
}

Upvotes: 2

Views: 1991

Answers (1)

user1864610
user1864610

Reputation:

Remove the trailing comma in your expression here:

$arr = array("data"=>"[Date.UTC(".$date."), ".$pos." ],");
                                                      ^       Remove this

Then outside the loop use

$teststring = implode(",", $arr);

That should create your required string without the trailing comma

Upvotes: 2

Related Questions