julie julie - 24 days ago 6
PHP Question

PHP convert 2 dimensional array to string

I am trying to convert following array:

array (size=6)
0 =>
array (size=1)
1 => string '611' (length=3)
1 =>
array (size=1)
1 => string '610' (length=3)
2 =>
array (size=1)
1 => string '608' (length=3)
3 =>
array (size=1)
1 => string '607' (length=3)
4 =>
array (size=1)
1 => string '606' (length=3)
5 =>
array (size=1)
1 => string '605' (length=3)


Expected output:
611, 610, 608, 607, 606, 605


I tried to do this:

foreach ($array as $sub) {
$str = implode(',', $sub);
}


but I got
605


Could you explain what I am doing wrong

Answer Source

In your code there is one error in foreach. You always replace previous value, you just need to do:

 foreach ($array as $sub) {
     $str .= implode(',', $sub);
 }