Jump to content

Archived

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

ldsmike88

Easiest way to echo an array

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

Share this post


Link to post
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>";
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites


I assumed the array was
array(
[0] => "2001", "2002", "2003", "2004"
)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

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.