PHP:单独的数组用逗号 [英] PHP: Separate array with commas

查看:90
本文介绍了PHP:单独的数组用逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code应该从一个阵列后跟一个逗号和空格显示值的列表!不过,我不希望是最后一个有后一个逗号和空格。

例如,我想 TAG1,TAG2,TAG3 而不是 TAG1,TAG2,TAG3,

这是我的code:

 < PHP $学期= get_the_terms($ the_post-> ID,'posts_tags');
                                的foreach($条款$项){                                    回声$ term->名;回声,
                                ?}>


解决方案

  $输出=阵列();
的foreach($条款$项){
  $输出[] = $ term->名;
}
回声破灭('',$输出);

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);

这篇关于PHP:单独的数组用逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆