Removing the last comma from a PHP foreach loop

I am wondering how can I remove the trailing comma in this foreach loop in my PHP script running inside javascript.

<html> <head> <script type='text/javascript' src='https://www.google.com/jsapi'></script> <script type='text/javascript'> google.load('visualization', '1', {'packages': ['geochart']}); google.setOnLoadCallback(drawRegionsMap); function drawRegionsMap() { var data = google.visualization.arrayToDataTable([ ['Country','Blocks'], <?php foreach ($bans as $key => $value) print"['$key', $value],\n"; ?> ]); var options = { backgroundColor : '#555555', colors : ['#FFFF00', '#FF0000'] }; var chart = new google.visualization.GeoChart(document.getElementById('chart_div')); chart.draw(data, options); }; </script> </head> <body> <div id="chart_div" style="width: 900px; height: 500px;"></div> </body> </html> 

This causes a problem when I get the final comma, I tried the implode method and the rtrim method, but none of them work (I also never used these functions so that I could be wrong)

My implode method:

 <?php $resultstr = array(); foreach ($bans as $key => $value) print"['$key', $value],\n"; $resultstr = implode(",", $resultstr); ?> 

My rtrim method:

  <?php $resultstr = array(); foreach ($bans as $key => $value) print"['$key', $value],\n"; echo rtrim($resultstr, ","); ?> 

When compiled, it looks like this:

  <html> 




google.load ('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback (drawRegionsMap);

  function drawRegionsMap() { var data = google.visualization.arrayToDataTable([ ['Country','Blocks'], ['Japan', 11], ['United States', 45], ['Argentina', 1], ]); var options = { backgroundColor : '#555555', colors : ['#FFFF00', '#FF0000'] }; var chart = new google.visualization.GeoChart(document.getElementById('chart_div')); chart.draw(data, options); }; </script> </head> <body> <div id="chart_div" style="width: 900px; height: 500px;"></div> </body> </html> 
+4
source share
4 answers

What you should do instead of trimming the line after this does not generate a trailing comma:

 $not_first = false; foreach ($bans as $key => $value) { if ($not_first) { print(", "); } $not_first = true; print "['$key', $value]\n"; } 
+6
source

This could be a different approach:

 var data = google.visualization.arrayToDataTable(<?php $table = array(array('Country', 'Blocks')); foreach($bans as $key => $value) { $table[] = array($key, $value); } echo json_encode($table); ?>); 

I often use json_encode, so I don't need to care about all the values.

+4
source

You missed something:

 echo rtrim($resultstr, ","); 

You can also do:

 echo substr($resultstr, 0, -1); 
+2
source

You can create an array containing the [key, value] fragments, and then stick them with implode :

Php

 <?php $arr = array(); $arr['key1'] = 'val1'; $arr['key2'] = 'val2'; $arr['key3'] = 'val3'; function PrepareValues(&$item, $key) { $item = "['$key', $item]"; } array_walk_recursive($arr, "PrepareValues"); $resultstr = implode(",\n", $arr); print($resultstr); ?> 

Output

 ['key1', val1], ['key2', val2], ['key3', val3] 
0
source

All Articles