Create a comma-separated string from a single column of an array of objects

前端 未结 14 1408
醉话见心
醉话见心 2020-11-30 07:36

I\'m using a foreach loop to echo out some values from my database, I need to strip the last comma from the last loop if that makes sense.

My loop is just simple, as

14条回答
  •  被撕碎了的回忆
    2020-11-30 08:16

    $a[0] = 'John Doe';       
    $a[1] = 'Jason statham';       
    $a[2] = 'Thomas Anderson';
    $size = count($a);
    foreach($a as $key=>$name){
        $result .= $name;
        if($size > $key+1) $result .=', ';
    }
    echo $result;
    

提交回复
热议问题