In your case, $respostas
will always be the last element - in case D - because there is a rewrite of the $respostas
variable. The first loop contains A , in the second it changes to B ... so on until the last.
Use $respostas
as array
, so each $_POST["respostas"][$c]
will contain an index in the array. Then just use implode
to join the array with the right commas.
I've done an example in Ideone , the output is Resposta A, Resposta B, Resposta C, Resposta D
. Disregard the array's, they are just for illustration of implode usage.
$c = 0;
$POST = array( 'Resposta A' , 'Resposta B' , 'Resposta C' , 'Resposta D' );
foreach( array( 'A' , 'B' , 'C' , 'D' ) as $listar => $valor)
{
$respostas[] = $POST[$c];
$c++;
}
echo implode( ', ' , $respostas );