php - Converting a nummeric array to comma separated string -


i have array this:

[participants] => array     (         [0] => 9         [1] => 8         [2] => 12     ) 

i want put in variable this:

"9,8,12"

i tried code below not output anything:

$mem = ""; for($i = 0; $i < count($participants); $i++) {     $mem.$participants[$i]; }  echo $mem; 

what can problem?

the easiest way use implode:

implode(',', $participants); 

in code should have used concatenation this:

$mem .= $participants[$i].','; 

however, have preferred foreach this:

$mem = ""; foreach ($participants $key => $value){     $mem .= $value.','; } 

if you're looping in way, have rid of last comma when you're done:

$mem = rtrim($mem, ','); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -