开发者

PHP: Separate array with commas [duplicate]

This question already has answers here: How to comma separate the values of an array for display (3 answers) Closed 9 years ago.

I have this code that should display a list of values from an array followed by a comma and space! However I don't want the last one to have a comma and 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 ", ";
                     开发者_开发百科           } ?>


$output = array();
foreach($terms as $term){
  $output[] = $term->name;
}
echo implode(', ', $output);


It´s an build in php feature called implode -> http://php.net/manual/function.implode.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜