In your case, $respostas
will always be the last element - in case D - because there is a rewriting of the variable $respostas
. The first loop contains To, the second she changes to B... so on until the last.
Use $respostas
as array
, thus each $_POST["respostas"][$c]
will contain an index in the array. Then just use implode
to join the array with the appropriate commas.
I made an example at Ideone, the output is Resposta A, Resposta B, Resposta C, Resposta D
. Disregard the array’s, they are only for illustration of the use of implode.
$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 );