Removing last comma from a foreach loop
Asked Answered
T

9

18

I'm using a foreach loop to echo out some values from my database and separating each of them by commas. I don't know how to remove the last comma it adds on the last value.

My code is pretty simple, but I just can't seem to find the correct way of doing this:

foreach ($this->sinonimo as $s){ 
    echo '<span>'.ucfirst($s->sinonimo).',</span>';
}
Thrush answered 12/11, 2013 at 17:4 Comment(1)
Related (possible duplicate): How do I create a comma-separated list from an array in PHP?Crumpet
D
34

Put your values in an array and then 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 );

This will put commas inbetween each value, but not at the end. Also in this case the comma will be outside the span, like:

<span>Text1<span>, <span>Text2<span>, <span>Text3<span>
Dance answered 12/11, 2013 at 17:6 Comment(1)
Great Sébastien! This helped me alot, great way of seeing this so easily cant believe I was so blind to not see this solution. I need more years of experience hehehe.Thrush
T
9

Another approach for your code would be a bit logic:

hasComma = false;
foreach ($this->sinonimo as $s){ 
    if (hasComma){ 
        echo ","; 
    }
    echo '<span>'.ucfirst($s->sinonimo).'</span>';
    hasComma=true;
}
Tamarra answered 12/11, 2013 at 17:15 Comment(0)
P
7

I'll generally try to avoid adding logic for such use cases. The best method would be to store the values in an array and implode it with a comma.

However, you can add the commas by CSS which is much easier.

Your nice and clean loop:

foreach($collection->object as $data)
{
    echo "<span>" . $data->var . "</span>";
}

And for the CSS:

.selector span:after {
    content: ",";
}
.selector span:last-child:after {
    content: "";
}

This works for all major browsers including IE8 and newer.

Planchet answered 28/8, 2014 at 7:30 Comment(0)
L
7

Treat separators as prefixes before values, with the first one pre-set to '' (empty string), then set to ', ' after each element:

$s = '';
foreach ($names as $name) {
    echo $s . $name;
    $s = ', ';
}
Laflamme answered 11/5, 2021 at 9:11 Comment(0)
H
4

Laravel has a implode() function same as php implode().

You should use implode() function on Collection object:

$user->posts->implode('title', ', ');

output will be something like this:

Hello world!, First post, Second post
Hosmer answered 23/8, 2015 at 10:27 Comment(0)
E
3

Alternatively to @Sébastien's answer, you could do this:

echo "<span>".ucfirst($this->sinonimo[0]);
for($i = 1; $i < count($this->sinonimo); $i++) {
    echo "</span>, <span>" . ucfirst($this->sinonimo[$i]);
}
echo "</span>";

This doesn't need the extra array. It works by first printing the first element, then in the loop printing the intermediate segment followed by the next element, and then closes everything off with the end segment.

Elatia answered 12/11, 2013 at 17:10 Comment(0)
P
2

use substr() function

Put your final string in substr() function

 $string = "";
    while ($row = mysql_fetch_array($result)) {
      $string .= $name . ', ';
    }
    echo substr($string, 0, strlen($string) - 2);
Provocative answered 16/5, 2018 at 10:50 Comment(0)
C
0

Laravel implode() function for relational data.

Like post has many categories.

$user->posts->implode('categories.name', ', '); // In loop {{  $post->categories->name }}
Cowcatcher answered 3/10, 2016 at 12:1 Comment(0)
D
0

u can use trim function. suppose your string is like this

$str=1,2,3,4,5,6,7,8,

u can try this

echo trim($str, ",");

and the output is

1,2,3,4,5,6,7,8
Drona answered 1/1, 2017 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.