Title: Comma in foreach output
Last modified: August 21, 2016

---

# Comma in foreach output

 *  [lacheney](https://wordpress.org/support/users/lacheney/)
 * (@lacheney)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/variable-in-terms-query/)
 * Hi,
 * I’m trying to find posts of a certain type that share taxonomy terms with the
   current single post being displayed.
 * Here’s my code:
 *     ```
       $mentionslist = wp_get_post_terms($post->ID, 'employer-mention', array()); 
   
       if ($mentionslist) {
       $mentionsoutput = '';
         foreach($mentionslist as $mention) {
           $mentionsoutput = $mentionsoutput.','.$mention->term_id;
         }
       }
   
       echo $mentionsoutput;
       ```
   
 * The echo of $mentionsoutput returns: ,575,576,1636
 * What I would like to do is remove the comma which appears right at the beginning
   so that I can use the variable in a term ID array. Which part of my foreach is
   wrong?
 * Thanks,
 * Lewis

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/variable-in-terms-query/#post-4466466)
 * You always end up with an extra comma somewhere. You can add more logic to suppress
   the first one, or use string functions to remove the first or last comma. There’s
   really no need though. Feed the resulting string with the extra comma into `explode()`.
   The extra comma will result in an empty array element. In some cases, this is
   not a problem. If it is, feed the array through `array_filter()` without a callback
   argument. It will strip out any empty elements.

Viewing 1 replies (of 1 total)

The topic ‘Comma in foreach output’ is closed to new replies.

## Tags

 * [comma](https://wordpress.org/support/topic-tag/comma/)
 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [taxonomy](https://wordpress.org/support/topic-tag/taxonomy/)
 * [terms](https://wordpress.org/support/topic-tag/terms/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [12 years, 5 months ago](https://wordpress.org/support/topic/variable-in-terms-query/#post-4466466)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
