Build comma separated string in PHP Loop

后端 未结 4 653
抹茶落季
抹茶落季 2021-01-21 22:36

Hello Guys i need to do this,

I have a common loop

foreach ($stuffs as $stuff) {
echo $stuff;
}

Lets assume $stuff is an \'id\' of a my

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 22:59

    If you really wanna have the loop:

    $values = "";
    
    foreach ($stuffs as $stuff) {
        $values != "" && $values .= ",";
        $values .= $stuff;
     }
    
    echo $values;
    

    I suggest using implode, but the loop can really give you more power if you wanna do some further stuff.

提交回复
热议问题