dbrimlow Posted May 2, 2012 Share Posted May 2, 2012 Hi, everyone, been a while since I've visited I hope this is a common issue with php and javascript. (thanks in advance for any help ) I have a jscript slideshow and am using php to dynamically drop an array of photos with links into the jscript's "imagearray" section. The problem I have is avoiding the trailing comma after the last image array element. Here's how it should look. imagearray: [ ["http://mysite.com/photos/123-large/123a.jpg","http://mysite.com/info.php?list=564623"], ["http://mysite.com/photos/123-large/123b.jpg","http://mysite.com/info.php?list=564623"], ["http://mysite.com/photos/123-large/123c.jpg","http://mysite.com/info.php?list=564623"], ["http://mysite.com/photos/123-large/123e.jpg","http://mysite.com/info.php?list=564623"] //no trailing comma for last value in array ], My php array is: //trailing comma gets included in each value $url = "[\"http://mysite.com/photos/".$id."-large/".$photo."a".chr(96+$j).".jpg", \"http://http://mysite.com/info.php?list=".$rlink."\"],"; $photos[]=$url; Putting the array into the jscript gallery: <script type="text/javascript"> blah, blah; imagearray: [<?php echo "$photos[]";?>], blah, blah </script> How would I avoid (or strip out) the comma on that last array value? Thanks again, Dave Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 Don't put the , on the end of the string in the first place, and then use implode. Quote Link to comment Share on other sites More sharing options...
dbrimlow Posted May 2, 2012 Author Share Posted May 2, 2012 Thanks for replying. How would I put the comma on each string while avoiding the last using implode? I never used implode other than to add or remove an element to/from each string. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 Read the docs? What you said doesn't make sense. The entire purpose of implode(x, y) is to put X between each string in array Y. That's another way of saying "put the comma on each string while avoiding the last". You can't use implode to remove strings from strings, and it always puts them between, not on the end of each. edit: unless there is a javascript implode that is different. I am talking about php.net/implode. Quote Link to comment Share on other sites More sharing options...
dbrimlow Posted May 2, 2012 Author Share Posted May 2, 2012 THANKS, Got it! That did it. What a total "Duh" moment ... of COURSE it will just add the comma to all but the last ... . Dave Quote Link to comment 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.