Forum Moderators: coopster
$question1 = str_replace(";", ",", $question1);
$question2 = str_replace(";", ",", $question2);
And it works. But since I have a lot of fields, I thought it would be more efficient to parse them with a foreach loop. But that's where I'm having a problem. I tried this but it doesn't work:
foreach ($_POST as $k=>$v) {
$_POST[$k] = str_replace(';', ',', $v);
$_POST[$k] = $v;
}
Can anyone show me what I need to do to get this foreach loop working properly? THanks for any help.
foreach ($_POST as $k=>$v) {
$_POST[$k] = str_replace(';', ',', $v);
} str_replace returns a string with the replacements you specified, it doesn't actually modify the original variable. This alone...
str_replace(';', ',', $my_variable); $my_variable = str_replace(';', ',', $my_variable); Note that it is currently not possible to change the values of the array directly in such a loop. A workaround is the following [php.net].
foreach ($_POST as $k=>$v) {
// won't work:
//$v = str_replace(';', ',', $v);
// works:
$_POST[$k] = str_replace(';', ',', $v);
} <edit>Elaborated</edit>
[edited by: coopster at 1:12 am (utc) on Feb. 27, 2004]
Is there an easier way to assign the results back to the variables after the str_replace has run instead of doing them one variable at a time with your $my_variable example? It seems kind of pointless to do the foreach loop if I have to go through each variable individually to assign the results back to the variable.
I tried to elaborate in the post so as to help you understand how it works and eliminate any confusion. Seems I did just the opposite!