How to get rid of the comma?

Good afternoon, Form so the line:
$gochart = '['.$row['id'].', '.$row['rulink'].', '.$row['ruquest'].', '.$row['rquest'].', '.$row['rans'].'],'; 
echo $gochart;

It is necessary to: [1, 0, 0, 0, 0],[2, 0, 0, 0, 0],[3, 26, 50, 5, 7],[4, 26, 60, 7, 11],[5, 67, 100, 6, 9],[6, 50, 37, 0, 10],[7, 54, 82, 9, 12],[8, 32, 74, 6, 9],[9, 31, 73, 9, 10],[10, 25, 76, 0, 6]

I get [1, 0, 0, 0, 0],[2, 0, 0, 0, 0],[3, 26, 50, 5, 7],[4, 26, 60, 7, 11],[5, 67, 100, 6, 9],[6, 50, 37, 0, 10],[7, 54, 82, 9, 12],[8, 32, 74, 6, 9],[9, 31, 73, 9, 10],[10, 25, 76, 0, 6],
With the comma at the end, how to get rid of the comma?
Thank you.
July 9th 19 at 10:21
6 answers
July 9th 19 at 10:23
Solution
Keep it simple

$gochart = array_values($row);
echo json_encode($gochart);


The point is that your string output is json data.
In php form the desired array, then drive it into json_encode()

upd
You have there some cycle I guess

$result = array();
cycle() {
...
 $result[] = array_values($row);
}

echo json_encode($result);
Even if no json, then just implod-ing array. - cassie_Doyl commented on July 9th 19 at 10:26
: Yes, so did - Earline commented on July 9th 19 at 10:29
the most beautiful solution! - Earline commented on July 9th 19 at 10:32
July 9th 19 at 10:25
Solution
<?php
$gochart = "[$row[id] $row[rulink], $row[ruquest], $row[rquest], $row[rans]]";
echo $gochart;
?>

or
<?php
$gochart = '['.$row['id'].', '.$row['rulink'].', '.$row['ruquest'].', '.$row['rquest'].', '.$row['rans'].'],';
echo rtrim($gochart, ',');
?>
July 9th 19 at 10:27
$row['rans'].'],';


remove

and generally it is better to rewrite the whole code
If it to remove, will not be commas between the [square brackets], and should only last.

[1, 0, 0, 0, 0][2, 0, 0, 0, 0][3, 26, 50, 5, 7][4, 26, 60, 7, 11][5, 67, 100, 6, 9][6, 50, 37, 0, 10][7, 54, 82, 9, 12][8, 32, 74, 6, 9][9, 31, 73, 9, 10] - cassie_Doyl commented on July 9th 19 at 10:30
July 9th 19 at 10:29
In fact there are many solutions. The easiest way to take a substring of length length-1. Here is a link to the function will not write itself, because I'm advocating that novices studied, not copied.
www.php.su/substr
July 9th 19 at 10:31
Trim the last character like this:
mb_substr($gochart, 0, -1);

but it's easier when generating the substitute is no comma at the end and the beginning, ie:
if (empty($gochart)) $gochart=$row['xxx'];
else $gochart.=', '.$row['xxx'];

And the first pass is not to substitute.
So mb_substr($gochart, 0, -1); tried to end it removes all the commas between [square brackets], and should only last.

[1, 0, 0, 0, 0][2, 0, 0, 0, 0][3, 26, 50, 5, 7][4, 26, 60, 7, 11][5, 67, 100, 6, 9][6, 50, 37, 0, 10][7, 54, 82, 9, 12][8, 32, 74, 6, 9][9, 31, 73, 9, 10] - cassie_Doyl commented on July 9th 19 at 10:34
July 9th 19 at 10:33
What not to do for the plus sign.

Find more questions by tags PHP