从foreach循环中删除最后一个逗号 [英] Removing last comma from a foreach loop
问题描述
$ b使用foreach循环来回显我的数据库中的一些值,并用逗号分隔每个值,但是我不知道如何去除最后一个逗号。 $ b
我的代码很简单,但我似乎无法找到这样做的正确方法: 感谢您的帮助:) 把你的值放在一个数组中,然后用逗号(+ 这会在每个值之间插入逗号,但不会结束。在这种情况下,逗号将超出范围,例如: Im using a foreach loop to echo out some values from my database and seperating each of them by commas but I dont know how to remove the last comma it adds on the last value. My code is pretty simple but I just cant seem to find the correct way of doing this: Thanks in advance for any help :) Put your values in an array and then This will put commas inbetween each value, but not at the end. Also in this case the comma will be outside the span, like:
这篇关于从foreach循环中删除最后一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ $ p $ foreach $ this-> sinonimo as $ s){
echo'< span>'。ucfirst($ s-> sinonimo)。',< / span>';
$ p
$ b
$ myArray = array();
foreach($ this-> sinonimo as $ s){
$ myArray [] ='< span>'ucfirst($ s-> sinonimo)。'< / span>' ;
}
echo implode(',',$ myArray);
< span> Text1< span>,< span> ; Text2< span>,< span> Text3< span>
foreach ($this->sinonimo as $s){
echo '<span>'.ucfirst($s->sinonimo).',</span>';
}
implode
that array with a comma (+ a space to be cleaner):$myArray = array();
foreach ($this->sinonimo as $s){
$myArray[] = '<span>'.ucfirst($s->sinonimo).'</span>';
}
echo implode( ', ', $myArray );
<span>Text1<span>, <span>Text2<span>, <span>Text3<span>