ldsmike88 Posted May 30, 2006 Share Posted May 30, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10752-easiest-way-to-echo-an-array/ Share on other sites More sharing options...
Ferenc Posted May 30, 2006 Share Posted May 30, 2006 [!--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>";} Quote Link to comment https://forums.phpfreaks.com/topic/10752-easiest-way-to-echo-an-array/#findComment-40162 Share on other sites More sharing options...
poirot Posted May 30, 2006 Share Posted May 30, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/10752-easiest-way-to-echo-an-array/#findComment-40165 Share on other sites More sharing options...
Ferenc Posted May 30, 2006 Share Posted May 30, 2006 I assumed the array was array( [0] => "2001", "2002", "2003", "2004") Quote Link to comment https://forums.phpfreaks.com/topic/10752-easiest-way-to-echo-an-array/#findComment-40167 Share on other sites More sharing options...
ldsmike88 Posted May 30, 2006 Author Share Posted May 30, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/10752-easiest-way-to-echo-an-array/#findComment-40178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.