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>';
}
1

9 Answers 9

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>
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. Commented Nov 12, 2013 at 17:20
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;
}
0
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.

0
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 = ', ';
}
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
0
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.

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

Laravel implode() function for relational data.

Like post has many categories.

$user->posts->implode('categories.name', ', '); // In loop {{  $post->categories->name }}
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.