将数组转换为一个长字符串

时间:2011-08-01 10:23:52

标签: php arrays string loops string-concatenation

如果我在php中有一个包含简单列表的数组,例如:

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

是否有函数或方法循环数组以使变量等于结果的STRING?
例如:

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

干杯!

5 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

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

不起作用。这是一个错误。这是正确的:

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

然后

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

答案 2 :(得分:0)

$theseColors  = implode(', ',$colors);

答案 3 :(得分:0)

Join / implode...

$theseColors = implode(', ', $colors);

答案 4 :(得分:0)

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