When building a JSON string from a php for loop from an SQL query there is an unnessary comma at the end of the build string. How do I control the final comma.
$count = label::grabAll()->count();
echo '{"data": { "graph": {';
for ($x = 0; $x < $count; $x++){
if($x <= 3){
$cm = ',';
}else{
$cm .= '';
}
echo '"'.$x.'": "'.label::grabAll()->results()[$x]->count.'"'.$cm;
}
I get this result from the code above.
How do I remove the final comma?