2011-08-01 36 views
0

如果我在PHP中的数组包含一个简单的列表例如:谈到阵列成一个长串

$colors = $array(); 
$colors = 'black', 'white', 'blue', 'red'; 

是否有一个功能或通过数组的方式进行循环,使一个变量等于一个STRING的结果?
例如:

$theseColors = 'black, white, blue, red'; 

干杯!

+1

http://www.php.net/manual/en/function.implode.php – Gordon

+2

问多余的重复之前,请做一些研究或者通过查看PHP手册很容易回答的问题。看到http://stackoverflow.com/questions/ask-advice – Gordon

+0

可能重复[合并数组项目到字符串](http://stackoverflow.com/questions/4626732/merge-array-items-into-string) –

回答

0
$theseColors = implode(', ',$colors); 
1
$colors = $array(); $colors = 'black', 'white', 'blue', 'red'; 

将无法​​正常工作。这是一个错误。这是正确的:

$colors = array(); $colors = array('black', 'white', 'blue', 'red'); 

然后

$theseColours = implode(", ", $colors); 
+0

@ stereofrog,好的,编辑 – k102

0
foreach ($color as $colors) { 
    $theseColours = $theseColours . $color; 
} 
+0

你试图发明一个自动。和你的代码是错误的。 – k102