-1

I want to remove the last comma from below array.

{"location":"Park Town,Chennai,Tamil Nadu" }, 
{"location":"Arajar Salai,Simmakkal,Madurai" },
SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
techiva.blogspot.com
  • 1,170
  • 3
  • 17
  • 37

2 Answers2

1

Jon Stirling have right in his comment. If you try to create json manually - it's bad idea.

But anwser to your question should be something like this (didn't test it) :

<?php
$tab=explode(",",$string);
array_pop($tab);
$string=implode(",",$tab).']';
?>
Michał G
  • 2,234
  • 19
  • 27
  • Hi it's returning same thing my actual output is -> {"location":"Park Town,Chennai,Tamil Nadu" },{"location":"Arajar Salai,Simmakkal,Madurai" }, this after that I am passing this on my view page google map inside array then it becomes like this [{"location":"Park Town,Chennai,Tamil Nadu" },{"location":"Arajar Salai,Simmakkal,Madurai" },] – techiva.blogspot.com Aug 23 '17 at 09:31
  • https://gist.github.com/poznet/e5b7857855f6366dcc6b65bf3ddbf031 gives me back string without last comma – Michał G Aug 23 '17 at 09:37
  • {"location":"Park Town,Chennai,Tamil Nadu" }]{"location":"Arajar Salai,Simmakkal,Madurai" }] returning like this – techiva.blogspot.com Aug 23 '17 at 09:41
  • can we remove the last comma from this string {"location":"Park Town,Chennai,Tamil Nadu" },{"location":"Arajar Salai,Simmakkal,Madurai" }, – techiva.blogspot.com Aug 23 '17 at 09:42
  • send whole php code where you generate this string – Michał G Aug 23 '17 at 10:05
0

Just use php rtrim() function on the string you will get your result.

like below

<?php 

$text = '{"location":"Park Town,Chennai,Tamil Nadu" },{"location":"Arajar Salai,Simmakkal,Madurai" },';


$test = rtrim($text,',');
echo $test;
?>

A live example https://eval.in/849038

Saroj
  • 1,343
  • 4
  • 15
  • 31
  • location is not static , location is generating dynamically right now two record is appearing after some time 3 or 4 records will appear – techiva.blogspot.com Aug 23 '17 at 10:40
  • OK. That's not a problem. We can trim the last comma after formation of your string that is outside of your loop. – Saroj Aug 23 '17 at 10:43