Jump to content

Easiest way to echo an array


ldsmike88

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!

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.