JDest Posted September 3, 2010 Share Posted September 3, 2010 This is going to sound like a very beginner question, because it is. I am modifying some free code snippets to fit my use and stuck. It's probably a simple fix. First here is the code. This is printing from a while loop. <?php print("[". $current_month .",". number_format($remaining_balance, "2", ".", ",") . "],");?> Here is the last remaining items from the loop at it comes to an end: [343,14,495.86], [344,13,682.26], [345,12,863.91], [346,12,040.79], [347,11,212.87], [348,10,380.12], [349,9,542.52], [350,8,700.02], [351,7,852.61], [352,7,000.26], [353,6,142.94], [354,5,280.62], [355,4,413.26], [356,3,540.85], [357,2,663.34], [358,1,780.72], [359,892.95], [360,0.00], My question is kind of simple, how do I get it do the comma doesn't print on the last print? I need it to end like this: [359,892.95], [360,0.00] Does that make sense? I don't know if I explained it well. I could really use some help! Thanks so much! Link to comment https://forums.phpfreaks.com/topic/212454-strip-comma-at-end-of-loop/ Share on other sites More sharing options...
taquitosensei Posted September 3, 2010 Share Posted September 3, 2010 something like this $glue=""; foreach($yourarray as $item) { print($glue."[". $current_month .",". number_format($remaining_balance, "2", ".", ",") . "]"); $glue=","; } Link to comment https://forums.phpfreaks.com/topic/212454-strip-comma-at-end-of-loop/#findComment-1106898 Share on other sites More sharing options...
muzzs Posted September 3, 2010 Share Posted September 3, 2010 Hi JDest, The way I would do this is buffer the output into a variable which will then allow you to strip the last , before you print it out as the final step. E.g. <?php $buffer = ""; while($current_month<10) { $buffer .= "[". $current_month .",". number_format($remaining_balance, "2", ".", ",") . "],"; } print(substr($buffer, 0, -1)); //Remove the last character and print ?> Hope that helps! Link to comment https://forums.phpfreaks.com/topic/212454-strip-comma-at-end-of-loop/#findComment-1106900 Share on other sites More sharing options...
PradeepKr Posted September 3, 2010 Share Posted September 3, 2010 I would first confirm that the last character is comma itself, before trimming it. Just as a precaution that I don't accidentally delete any other character, <?php $buffer = ""; while($current_month<10) { $buffer .= "[". $current_month .",". number_format($remaining_balance, "2", ".", ",") . "],"; } if( substr("abcdef", -1) == ',') print(substr($buffer, 0, -1)); //Remove the last character and print ?> Link to comment https://forums.phpfreaks.com/topic/212454-strip-comma-at-end-of-loop/#findComment-1106999 Share on other sites More sharing options...
sasa Posted September 4, 2010 Share Posted September 4, 2010 rtrim($buffer,','); Link to comment https://forums.phpfreaks.com/topic/212454-strip-comma-at-end-of-loop/#findComment-1107103 Share on other sites More sharing options...
ignace Posted September 4, 2010 Share Posted September 4, 2010 Either of these is the solution your looking for: something like this $glue=""; foreach($yourarray as $item) { print($glue."[". $current_month .",". number_format($remaining_balance, "2", ".", ",") . "]"); $glue=","; } rtrim($buffer,','); Link to comment https://forums.phpfreaks.com/topic/212454-strip-comma-at-end-of-loop/#findComment-1107125 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.