CyberShot Posted April 8, 2011 Share Posted April 8, 2011 I am using a foreach loop to get the results of an array. My problem is that I need the results combined into one string. How can I accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/ Share on other sites More sharing options...
requinix Posted April 8, 2011 Share Posted April 8, 2011 One of a billion ways. Can you be more specific? Maybe even post code? Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/#findComment-1198529 Share on other sites More sharing options...
drisate Posted April 8, 2011 Share Posted April 8, 2011 If i got it right ... you wana do something like this? foreach ($array[key] as $key => $value){ $string .= $value.", "; } $string = substr($string, 0, -2); // Delete the last 2 characters echo $string; I would require more of your code in order to show you exacly how it's done but the above is an exemple. Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/#findComment-1198531 Share on other sites More sharing options...
CyberShot Posted April 8, 2011 Author Share Posted April 8, 2011 foreach($options['exc_cat'] as $cat) { echo $cat; } this code returns 1 5 I want it to display as 1,5 I have been searching and tried substr($cat, 0, -1) but this strips all the commas from the string. the way I am returning the value, it prints as 1,5, I need the last comma after the 5 to go away. But the substr strips all commas which just donned on me that the foreach loop loops through, returns 1, then loops again and returns 5, I want to combine the total overall results into one string so that I can strip the trailing comma. Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/#findComment-1198532 Share on other sites More sharing options...
CyberShot Posted April 8, 2011 Author Share Posted April 8, 2011 close, that gave me 551 instead of 5,1 Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/#findComment-1198534 Share on other sites More sharing options...
drisate Posted April 8, 2011 Share Posted April 8, 2011 do it like this foreach($options['exc_cat'] as $cat) { if ($cat!=""){$string .= $cat.",";} } $string = substr($string, 0, -1); // Delete the last character echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/#findComment-1198535 Share on other sites More sharing options...
CyberShot Posted April 8, 2011 Author Share Posted April 8, 2011 Absolutely perfect my friend! That was an even better solution than I was hoping for. It worked perfect! I had to use the results in a wordpress query and getting the results out of the foreach loop made it great. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/#findComment-1198536 Share on other sites More sharing options...
drisate Posted April 8, 2011 Share Posted April 8, 2011 No prob ;-) Enjoy! Quote Link to comment https://forums.phpfreaks.com/topic/233037-help-with-foreach-loop-results/#findComment-1198537 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.