PHP:用逗号分隔数组

Cam*_*ron 6 php arrays

我有这个代码应该显示一个数组列表,后面跟一个逗号和空格!但是我不希望最后一个在它之后有逗号和空格.

所以例如我想要tag1, tag2, tag3而不是tag1, tag2, tag3,

这是我的代码:

<?php $terms = get_the_terms( $the_post->ID, 'posts_tags' );
                                foreach ( $terms as $term ) {

                                    echo $term->name;echo ", ";
                                } ?>
Run Code Online (Sandbox Code Playgroud)

Roc*_*mat 21

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

  • @Marek:确实如此.`implode(',',$ array)`. (5认同)