-1

If I have an array in php that contains a simple list e.g:

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

Is there a function or a way to loop through the array to make a variable equal a STRING of the results?
For example:

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

Cheers!

3

5 Answers 5

3

Try this,

http://php.net/manual/en/function.implode.php.

Sign up to request clarification or add additional context in comments.

Comments

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

won't work. it's an error here. this is correct:

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

and then

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

1 Comment

@stereofrog, ok, made an edit
0
$theseColors = implode(', ',$colors); 

Comments

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

1 Comment

you are trying to invent a bycicle. and your code is wrong.
0

Join / implode...

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.