Remove the last part of the comma in foreach in PHP?

Remove the last part of the comma in foreach in PHP?

By : user2173854
Date : October 19 2020, 08:10 PM
To fix this issue How can I prevent the insertion of a comma at the end of the title3? , just use implode() - which is the equivalent to .join():
code :
echo implode(',', $_POST['titles']);
echo implode($_POST['titles']);
// $titles = explode(',', $_POST['titles']);
$titles = $_POST['titles'];
for($i=0; $i < sizeof($titles); $i++) {
    echo $titles[$i];
    if($i+1 != sizeof($titles)){echo ',';}

Share : facebook icon twitter icon
Powershell - Remove last comma in foreach

Powershell - Remove last comma in foreach

By : Athanasius Alekseyev
Date : March 29 2020, 07:55 AM
hop of those help? JSON is not a flat string, never try to build it by concatenating flat strings and hoping that it will come out all-right in the end.
JSON is a serialized data structure, treat it as such. Make a data structure and serialize it.
code :
$result = @{}
$result.data = @()

foreach ($vm in Get-VM) {
    $result.data += @{
        "{#VMNAME}" = $vm.Name;
        "{#VMSTATE}" = $vm.State;

$result | ConvertTo-Json
    "data" = Get-VM | % { 
            "{#VMNAME}" = $_.Name;
            "{#VMSTATE}" = $_.State;
} | ConvertTo-Json
Remove comma on last item of foreach

Remove comma on last item of foreach

By : t3o79
Date : March 29 2020, 07:55 AM
Any of those help I have next code for WordPress loop of tags for single post: , Check if your loop is working on the last one:
code :
<?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; ?>
        <?php if ($i < $count - 1) echo ", "; ?>
    <?php endforeach; ?>
<?php endif; ?>
Remove foreach last comma PHP

Remove foreach last comma PHP

By : Maamohth
Date : March 29 2020, 07:55 AM
wish of those help 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.
code :
$output = array();
foreach($result as $r) {
    $output[] = $r['lenght'];

echo implode(',', $output);
How to remove last comma from each array in foreach?

How to remove last comma from each array in foreach?

By : Andrey
Date : March 29 2020, 07:55 AM
like below fixes the issue Here is a less-loopy method that uses array_splice() instead of array_chunk(). No count() calls, no incrementing counters, just one loop.
Code: (Demo)
code :
$cars=array( "Volvo","BMW","Toyota","Honda","Mercedes");
while($cars){                             // while there are still elements in the array...
    $chunk=array_splice($cars,0,2);       // extract first two elements (reducing $cars)
    if(isset($chunk[1])){$chunk[0].=',';} // add comma to 1st element if 2nd element exists
    $output[]=$chunk;                     // add extracted elements to multi-dim output
array (
  0 => 
  array (
    0 => 'Volvo,',
    1 => 'BMW',
  1 => 
  array (
    0 => 'Toyota,',
    1 => 'Honda',
  2 => 
  array (
    0 => 'Mercedes',
$cars=array( "Volvo","BMW","Toyota","Honda","Mercedes");
    echo "<ul><li>",implode(",</li><li>",$chunk),"</li></ul>";  // add comma to implode's glue string
How to remove the last comma from foreach loop?

How to remove the last comma from foreach loop?

By : Gilles Wonder
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further 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
code :
$out = '';

foreach ($snippet_tags as $tag_data) {
    $tags_id = $tag_data->tag_id;
    $tagsdata = $this->Constant_model->getDataOneColumn('tags', 'id', $tags_id);

    // 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, ',');
Related Posts Related Posts :
  • Running two PHP versions on the same server (STRETCH)
  • Checking if a file has been selected for upload
  • Model not updating laravel
  • How to add a description of words from text to an array, to an array key by passing words?
  • Nginx rewrite rule not not passing variables if "/" is at the end of the url
  • Modify decimal point and thousands separator without changing the number of decimals
  • Remove PHP errors from CLI and PHPMyAdmin
  • Yii2 rest api - Url manager add parameters
  • 1-query batch update array of users using PDO
  • Keep getting error: implode(): Invalid arguments passed when updating data
  • Laravel 5.4 - Correct Validation rule for a required parameter that can be zero
  • What does this code mean: $objPHPExcel->getActiveSheet()->toArray(null, true,true,true)
  • Data not being added to mysql table but echo's the data?
  • While fetch row in PHP
  • IP long string filter
  • How to view first image in list per user as its highlight Profile
  • Insert multiple rows from array
  • PHP : How to remove a duplicate array item ONCE
  • Print the input box text as simple text instead of html tag
  • How to read a string variable in php as $_GET or $_POST?
  • MySQL query fetch array not working
  • LogicException with message '... must return a relationship instance.'
  • PHP - create new array with only selected keys from an existing array
  • How to allow backgrounding a process to survive a session termination?
  • How to redirect to Home page (Default page) in zend framework 2?
  • I want to call a jquery variable in php syntax
  • Disabling the default /?add-to-cart= animation on buttons in Woocommerce
  • How to use query data for another sql query in yii2
  • Replace string enclosed in specified characters based on data type
  • Symfony FormType: how to get prototype in form type file
  • Handle reply messages properly after some operations in php
  • PHP Changed Domain, how to remove old Session Cookies
  • mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in php
  • Error: Call to a member function update() on array laravel
  • How to change the format of a float number?
  • run ajax requests by order
  • Wordpress Install 404 Error (Cannot view login in browser)
  • Calcinai xero-php pass config once
  • Fatal error calling to undefined function in PHP file
  • Laravel hasMany and belongsTo parameters
  • How do I call a Controller method in php Laravel?
  • How to replace "@" post tag with regex in PHP?
  • Laravel : when insert record in table. How can i add that Record in the list without querying the db
  • It is possible that I could user email address to get customer id from simPRO API?
  • PHP add and merge sql result
  • laravel ckeditor dark theme
  • Laravel belongsToMany not working
  • laravel 5.4 datatables not working jquery $ referenceError
  • Zend Form 2 multiselect field is empty after validation
  • PHP - Laravel 5 get data to html table from 3 SQL database tables with column DATA as headers
  • Can't use token authorization with Laravel Passport
  • splitting lines of dates and presenting in desired format
  • Laravel 5.2 Heroku Deployment Failed to install system packages
  • Laravel relationships - are there multiple *unnecessary* database calls?
  • htaccess rewrite rule for serving index.php from parent directory
  • PHP/MongoDB - $concat aggregation
  • Remove ajax.googleapis.com from my Wordpress site
  • Image saved always empty with Storage::put in Laravel
  • How to count and sort data in junction table
  • Get common number of values in a single array
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org