Jump to content

help with foreach loop results


CyberShot

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.