Jump to content

how to avoid last array item comma


dbrimlow

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/261965-how-to-avoid-last-array-item-comma/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.