remove last character after concatenating string output by for loop?

后端 未结 3 1758
迷失自我
迷失自我 2021-01-17 03:28
$holder = \'\';

foreach($fields as $key){
    $holder .= $key.\', \';
}

echo $holder;

I have the code above, it outputs \"a, b, c, \" I want to r

相关标签:
3条回答
  • You can use implode() to join all array elements together :

    <?php
       $holder = implode(', ', $fields);
       echo $holder;
    ?>
    
    0 讨论(0)
  • 2021-01-17 03:30

    You can use substr like this

    $holder = '';
    
    foreach($fields as $key){
        $holder .= $key.', ';
    }
    $newholder=substr($holder, 0, -1); 
    echo $newholder;
    
    0 讨论(0)
  • 2021-01-17 03:50

    That's not how it's done.

    $holder = join(', ', $fields)
    
    0 讨论(0)
提交回复
热议问题