Jump to content

Recommended Posts

What is the easiest way to echo an array in a format like this:
"2001", "2002", "2003", "2004"

I can echo an array like this:
[code]foreach($exedYear as $exedYearValue){
echo '"' . $exedYearValue . '", ';
}[/code]

But then at the end of the array there is an extra comma and space. How do I get rid of the comma? Thanks!

Mike
Link to comment
https://forums.phpfreaks.com/topic/10752-easiest-way-to-echo-an-array/
Share on other sites

[!--quoteo(post=378280:date=May 29 2006, 09:08 PM:name=ldsmike88)--][div class=\'quotetop\']QUOTE(ldsmike88 @ May 29 2006, 09:08 PM) [snapback]378280[/snapback][/div][div class=\'quotemain\'][!--quotec--]
What is the easiest way to echo an array in a format like this:
"2001", "2002", "2003", "2004"

I can echo an array like this:
[code]foreach($exedYear as $exedYearValue){
echo '"' . $exedYearValue . '", ';
}[/code]

But then at the end of the array there is an extra comma and space. How do I get rid of the comma? Thanks!

Mike
[/quote]
explode() will do that for you.

$parts = explode(',', $exedYear);
foreach($parts as $key => $value){
echo trim($value) . "<br>";
}
Ferenc, I guess you misunderstood him. He wants to output an array and not convert an array into a string.

You can use implode for that:
[a href=\"http://www.php.net/implode\" target=\"_blank\"]http://www.php.net/implode[/a]

[code]echo implode(', ', $exedYear);[/code]
That's interesting. I have been using explode() in this script but never implode. The only problem with that though is that it doesn't include the parenthases around the variables. I tried to add them but it doesn't set the new variables, it jecho's the same thing: [code]foreach($exedYear as $years){
$years = '"' . $years . '"';
}
echo implode(', ', $exedYear);[/code]

I got it!

[code]echo '"' . implode('", "', $exedYear) . '"';[/code]

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