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? Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/ 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 Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981856 Share on other sites More sharing options...
dod Posted December 21, 2009 Author Share Posted December 21, 2009 why implode? Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981857 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. Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981858 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 Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981862 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. Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981865 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 Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981956 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? Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981960 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! Link to comment https://forums.phpfreaks.com/topic/185932-help-with-a-foreach/#findComment-981963 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.