dod Posted December 21, 2009 Share Posted December 21, 2009 foreach($array as $key => $value){ $returnValue .= $value.','; $value; } how do you make a loop like this, but the last item doesnt have a comma at the end? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2009 Share Posted December 21, 2009 use implode instead echo implode(',' , $array); http://php.net/manual/en/function.implode.php Quote Link to comment Share on other sites More sharing options...
dod Posted December 21, 2009 Author Share Posted December 21, 2009 why implode? Quote Link to comment Share on other sites More sharing options...
akitchin Posted December 21, 2009 Share Posted December 21, 2009 implode, if you read the manual entry on it, will glue every value in the array into one string using the delimiter specified (here, a comma) to separate values. this seems to achieve what you're trying to do. Quote Link to comment Share on other sites More sharing options...
dod Posted December 21, 2009 Author Share Posted December 21, 2009 but i need the last item to have now comma Quote Link to comment Share on other sites More sharing options...
akitchin Posted December 21, 2009 Share Posted December 21, 2009 have you even TRIED to use implode? run that line of code rajivgon has provided you, i'm sure you'll find it spits out exactly what you want. if it doesn't, post what is output and what is wrong about it. Quote Link to comment Share on other sites More sharing options...
dod Posted December 22, 2009 Author Share Posted December 22, 2009 here my function function select($array){ $last_item = end($array); $last_item = each($array); reset($array); foreach($array as $key => $value){ $returnValue .= $value.','; $value; } return($returnValue); } i need $returnValue to be a string for the site or it will not work and also fr the last item to not have a comma Quote Link to comment Share on other sites More sharing options...
trq Posted December 22, 2009 Share Posted December 22, 2009 Did you even read the replies already posted? Quote Link to comment Share on other sites More sharing options...
dod Posted December 22, 2009 Author Share Posted December 22, 2009 bloody hell that worked pefeatly, i hope! Quote Link to comment 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.