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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.