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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10936059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:24:18  来源:igfitidea点击:

How to convert items in array to a comma separated string in PHP?

phparrays

提问by Ahmed Fouad

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, ...';

回答by John Conde

Use implode:

使用内

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

回答by RutZap

Yes you can do this by using implode

是的,你可以通过使用内来做到这一点

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

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

只是你知道,implode有一个别名,叫做join

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

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

我倾向于使用 join 而不是 implode 因为它有一个更好的名字(一个更不言自明的名字:D)

回答by GDP

Use PHP function Implodeon your array

在数组上使用 PHP 函数内

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

回答by Mike Mackintosh

Simply implode:

简单地内爆:

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