如果我在PHP中的数组包含一个简单的列表例如:谈到阵列成一个长串
$colors = $array();
$colors = 'black', 'white', 'blue', 'red';
是否有一个功能或通过数组的方式进行循环,使一个变量等于一个STRING的结果?
例如:
$theseColors = 'black, white, blue, red';
干杯!
如果我在PHP中的数组包含一个简单的列表例如:谈到阵列成一个长串
$colors = $array();
$colors = 'black', 'white', 'blue', 'red';
是否有一个功能或通过数组的方式进行循环,使一个变量等于一个STRING的结果?
例如:
$theseColors = 'black, white, blue, red';
干杯!
$theseColors = implode(', ',$colors);
$theseColors = implode(', ', $colors);
$colors = $array(); $colors = 'black', 'white', 'blue', 'red';
将无法正常工作。这是一个错误。这是正确的:
$colors = array(); $colors = array('black', 'white', 'blue', 'red');
然后
$theseColours = implode(", ", $colors);
@ stereofrog,好的,编辑 – k102
foreach ($color as $colors) {
$theseColours = $theseColours . $color;
}
你试图发明一个自动。和你的代码是错误的。 – k102
http://www.php.net/manual/en/function.implode.php – Gordon
问多余的重复之前,请做一些研究或者通过查看PHP手册很容易回答的问题。看到http://stackoverflow.com/questions/ask-advice – Gordon
可能重复[合并数组项目到字符串](http://stackoverflow.com/questions/4626732/merge-array-items-into-string) –