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

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -