Remove comma on last item of foreach
Check if your loop is working on the last one:
<?php if ($tags) : ?>
<?php $count = count($tags); ?>
<?php foreach($tags as $i => $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>
<?php if ($i < $count - 1) echo ", "; ?>
<?php endforeach; ?>
<?php endif; ?>
Remove comma from last result in nested forEach loop
I'd use map/join:
const email = "john@gmail.com";const body = { "fruit": ["apple"], "vegetables": ["carrot", "beans"], "dairy": ["milk", "cheese"]};
const values = Object.keys(body) .map(key => body[key] .map(item => `('${item}', '${key}', '${email}')`) .join(', ') ) .join(', ');
console.log(values);
How to remove the last comma from foreach loop?
By placing the HTML in a simple string variable and then using rtrim()
on the resulting string before outputting it this should remove the final ,
from the string
<?php
$out = '';
foreach ($snippet_tags as $tag_data) {
$tags_id = $tag_data->tag_id;
$tagsdata = $this->Constant_model->getDataOneColumn('tags', 'id', $tags_id);
$tag_name=$tagsdata[0]->tag_name;
// move inside loop and amend to place in a simple string var
$out .= '<a href="' . base_url() . 'tags/' . $tag_name . '">' . $tag_name . '</a>,';
?>
echo rtrim($out, ',');
Removing last comma from a foreach loop
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>
How to remove comma in last item of foreachloop in laravel?
Try to do this
<?php $i = 1; $len = count($product->Offers); ?>
@foreach($product->Offers as $offer)
{
"@type": "Offer",
"name": "{{$offer->title}}",
"price": "{{$offer->displayPrice}}",
}<?php if($i < $len){echo ',';} $i++;?>
@endforeach
Removing Last Comma in a For & Foreach Loop
Do not display the text immediately. "Cache it" and then display it, but remove the last character either by substr or rtrim
for ($i = 0; $i < $compatibilitycount; $i++) {
$result = '';
foreach ($xml->Item->ItemCompatibilityList->Compatibility[$i] as $compatibility ) {
if ($compatibility->Value != '') {
$result .= $compatibility->Value . ',';
}
}
echo substr($result, 0, -1) . '<br />';
// or echo rtrim($result, ',') . '<br />';
}
Remove foreach last comma PHP
One way to solve this is to not echo out the commas in the foreach
loop. Put the data you want to echo into an array, then use implode
.
$output = array();
foreach($result as $r) {
$output[] = $r['lenght'];
}
echo implode(',', $output);
How to remove the last comma from the last item generated from a foreach loop, inside a function
Simply trim() it with your own character list
foreach ($jsonbreadcrumbs as $crumb) {
$link = ucwords(str_replace(array(".php","-","_"), array(""," "," "), $crumb));
$root_domain .= $crumb . '/';
$jsonbreadcrumb .= '{"@type": "ListItem",';
$jsonbreadcrumb .= '"position":' . $itemNumber++ . ',';
$jsonbreadcrumb .= "\"name\": \"{$link}\",";
$jsonbreadcrumb .= "\"item\": \"{$root_domain}\"},";//<---this last comma,when it belongs to the last item, needs to be removed.
}
$jsonbreadcrumb=trim($jsonbreadcrumb,","); // this removes that trailing comma...
$jsonbreadcrumb .= ']}</script>';
Related Topics
Send Python Output to HTML Field
Generate an N-Digit Random Number
Install PHP Intl Extension on Macos
Get Image Type from Base64 Encoded Src String
How to Change Value of Select Options Based on Another Select Option Selected
Getting a File to Download Instead of Opening the Browser
Mysql Return Result If 0 Rows Returned
Option Selected With Codeigniter and Data from Db
How to Get Username from Facebook Id
Laravel - Display a Pdf File in Storage Without Forcing Download
Check If Username and Email Already Exists in Database
How to Download an Mp3 File from Remote Url
How to Get the Sucess Message in the Same Page After Submitting the Contact Form