如何将数组中的项转换为PHP中的逗号分隔字符串?(复制)

时间:2022-01-11 00:17:57

Possible Duplicate:
How to create comma separated list from array in PHP?

可能重复:如何在PHP中从数组中创建逗号分隔的列表?

Given this array:

鉴于这个数组:

$tags = array('tag1','tag2','tag3','tag4','...');

How do I generate this string (using PHP):

如何生成这个字符串(使用PHP):

$tags = 'tag1, tag2, tag3, tag4, ...';

4 个解决方案

#1


71  

Use implode:

使用内爆:

 $tags = implode(', ', array('tag1','tag2','tag3','tag4'));

#2


15  

Yes you can do this by using implode

是的,你可以通过内爆实现

$string = implode(', ', $tags);

And just so you know, there is an alias of implode, called join

大家知道,内爆的别名是join

$string = join(', ', $tags);

I tend to use join more than implode as it has a better name (a more self-explanatory name :D )

我倾向于使用连接,而不是内爆,因为它有一个更好的名称(一个更自我解释的名称:D)

#3


11  

Use PHP function Implode on your array

在数组中使用PHP函数内爆

$mystring = implode(', ',$tags)

#4


6  

Simply implode:

简单的崩溃:

$tags = implode(", ", $tags);

#1


71  

Use implode:

使用内爆:

 $tags = implode(', ', array('tag1','tag2','tag3','tag4'));

#2


15  

Yes you can do this by using implode

是的,你可以通过内爆实现

$string = implode(', ', $tags);

And just so you know, there is an alias of implode, called join

大家知道,内爆的别名是join

$string = join(', ', $tags);

I tend to use join more than implode as it has a better name (a more self-explanatory name :D )

我倾向于使用连接,而不是内爆,因为它有一个更好的名称(一个更自我解释的名称:D)

#3


11  

Use PHP function Implode on your array

在数组中使用PHP函数内爆

$mystring = implode(', ',$tags)

#4


6  

Simply implode:

简单的崩溃:

$tags = implode(", ", $tags);