Endorphin Posted March 3, 2012 Share Posted March 3, 2012 Hi All, I'm sure there is an easy solution for this but I am unable to find it. I am new to PHP and after a little help... the via data is stored in a database in this format... |51.105166,-1.695971|51.011055,-2.1068|50.945233,-2.617664|||| I'm trying to find a way of loosing the last comma if there are 1 or more entries, any ideas guys. What I'm getting is : var points = [{location: '51.105166,-1.695971'},{location: '51.105166,-1.695971'},]; What I'm after is: var points = [{location: '51.105166,-1.695971'},{location: '51.105166,-1.695971'}]; <?php if($via != null){ echo "var points = ["; foreach($via as $point){ if($point != ""){ echo "{location:"; echo " '".$point."'"; echo "},"; } } echo "];\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/ Share on other sites More sharing options...
requinix Posted March 3, 2012 Share Posted March 3, 2012 Construct a multidimensional array that looks like what you want, then json_encode it. Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323521 Share on other sites More sharing options...
Endorphin Posted March 3, 2012 Author Share Posted March 3, 2012 Thanks for the quick reply requinix, as I said I am new to PHP but I will read through the link. Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323522 Share on other sites More sharing options...
SergeiSS Posted March 3, 2012 Share Posted March 3, 2012 You'd better do it in another way. The more accurate (and more compact) code is the next if($via != null) { $points=array(); foreach($via as $point) { if($point != "") $points[]="{location:'$point'}"; } if( count( $points > 0 ) ) echo "var points = [".implode( ',', $points)."];\n"; Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323540 Share on other sites More sharing options...
requinix Posted March 3, 2012 Share Posted March 3, 2012 You'd better do it in another way. I have a question for you. If you also believe that it should be done another way, and I'm sure you've seen that OP is trying to learn that better way, why did you give him/her the bad answer? Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323553 Share on other sites More sharing options...
SergeiSS Posted March 3, 2012 Share Posted March 3, 2012 I have a question for you. If you also believe that it should be done another way, and I'm sure you've seen that OP is trying to learn that better way, why did you give him/her the bad answer? What is wrong in my answer? Endorphin tries to create a list of comma-separated values. His task was to remove the last comma. Am I right? And I show the way how to create a string without comma at it's end. Do you agree? OK, again - where my answer is wrong? Even if he would like to use multidimensional array, he can use my code. He just need to change it a little. Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323561 Share on other sites More sharing options...
requinix Posted March 3, 2012 Share Posted March 3, 2012 Your answer is right, it's the best way to build a string like that without having a trailing comma that needs to be removed. I'm just saying that using something like json_encode(), which is for situations exactly like this one, is better. Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323564 Share on other sites More sharing options...
SergeiSS Posted March 3, 2012 Share Posted March 3, 2012 I think there is a "slight" difference between "you give him/her bad anwswer", "your answer is right" and "using something like json_encode() ... is better". Do you agree with me? Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323569 Share on other sites More sharing options...
requinix Posted March 3, 2012 Share Posted March 3, 2012 If the choice is between "good" and "bad" then only one of those answers was the "good" answer. That wasn't to say it wasn't correct or right, but as far as I'm concerned it wasn't good. But now I'm just splitting hairs. Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323572 Share on other sites More sharing options...
Endorphin Posted March 4, 2012 Author Share Posted March 4, 2012 I would like to thank both of you for your help with this. It's very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/258197-remove-the-last-comma/#findComment-1323723 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.