PHP: a separate array with commas

I have this code that should display a list of values ​​from an array, followed by a comma and a space! However, I do not want the latter to have a comma and a space after it.

So, for example, I want tag1, tag2, tag3 instead of tag1, tag2, tag3,

This is my code:

 <?php $terms = get_the_terms( $the_post->ID, 'posts_tags' ); foreach ( $terms as $term ) { echo $term->name;echo ", "; } ?> 
+6
arrays php
source share
2 answers
 $output = array(); foreach($terms as $term){ $output[] = $term->name; } echo implode(', ', $output); 
+20
source share

This is a built-in php function called implode -> http://php.net/manual/function.implode.php.

+1
source share

All Articles