PHP add single quotes to comma separated list

前端 未结 7 1130
逝去的感伤
逝去的感伤 2021-02-05 05:55

When I implode my array I get a list that looks like this:

qwerty, QTPQ, FRQO

I need to add single quotes so it looks like:

\'         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 06:33

    Similar to what Rizier123 said, PHP's implode method takes two arguments; the "glue" string and the "pieces" array.

    so,

    $str = implode(", ", $arr);
    

    gives you the elements separated by a comma and a space, so

    $str = implode("', '", $arr);
    

    gives you the elements separated by ', '.

    From there all you need to do is concatenate your list with single quotes on either end.

提交回复
热议问题