alconebay Posted October 8, 2008 Share Posted October 8, 2008 I have about 100 variables that are holding currency information that needs to be summed up. The users always format the currency with commas but the commas are treated like a decimal (messing up the sum) so I need to run str_replace on all the variables to remove the commas. Code example: $variable1 $variable2 $variable3 $variable1 = str_replace(",", "", $variable1); $variable2 = str_replace(",", "", $variable2); $variable3 = str_replace(",", "", $variable3); My question: Do I have to make a str_replace line for each of the 100 variables or is there a way I can tell it to run str_replace on the following variables (then have all the variables listed as comma separated values or something like that)? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 8, 2008 Share Posted October 8, 2008 Would be a tonne easier if you had used arrays to store the information. I'm sorry but I don't know too much about Variable Variables and if they could help you in the state you are in. Otherwise, I think you MAY need to use str_replace() on them all. But really, it could've easily been avoided if you had structured your code better, using arrays. Sorry. Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted October 8, 2008 Share Posted October 8, 2008 $variable1="123,456"; $variable2="789,011"; $variable3="719,022"; $array = array($variable1,$variable2,$variable3); print_r( str_replace(",","",$array)); Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 8, 2008 Share Posted October 8, 2008 You shouldn't have to make the array out of the variables, the variables should've been made an array when constructed. But still, it's an option, although you could just as well go through and do an str_replace() for all of then. Quote Link to comment Share on other sites More sharing options...
philipolson Posted October 8, 2008 Share Posted October 8, 2008 I agree an array would be better, but just for fun: <?php $variable1="123,456"; $variable2="789,011"; $variable3="719,022"; for ($i = 1; $i <= 100; $i++) { ${'variable' . $i} = str_replace(',','', ${'variable' . $i}); } ?> It works but no error checking, fairly useless, inefficient... but that's how I roll. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 8, 2008 Share Posted October 8, 2008 If your variables are sequenced like in your example, you can use variable variables: <?php for ($i = 1; $i <= 100; $i++) { $var_name = "variable$i"; $$var_name = str_replace(',', '', $$var_name); } ?> But still, try to fill an array with the values instead of creating a variable for each. Edit: Guess philip beat me to it Quote Link to comment Share on other sites More sharing options...
philipolson Posted October 8, 2008 Share Posted October 8, 2008 It's nice to show different ways to do something... like variable variables. Quote Link to comment Share on other sites More sharing options...
alconebay Posted October 8, 2008 Author Share Posted October 8, 2008 My variables aren't sequenced. Oh well, guess I'll go through and make a line for each one. I'll know to use an array next time though! Thanks for the help. Quote Link to comment Share on other sites More sharing options...
philipolson Posted October 8, 2008 Share Posted October 8, 2008 Well, you could also: <?php // These are your variable names $names = array('foo', 'bar', 'baz'); foreach ($names as $name) { $$name = str_replace(',', '', $$name); } echo "$foo $bar $baz"; ?> I'm too lazy to make it more efficient (am thinking creating array, and calling str_replace() once would be) but anyway... the above is easy. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 8, 2008 Share Posted October 8, 2008 @ philip You could use array_map() to convert the $names array to an array with the actual values. Guess it's faster than calling str_replace each time: <?php function return_var($var_name) { return $$var_name; } // These are your variable names $names = array('foo', 'bar', 'baz'); $vars = array_map('return_var', $names); $vars = str_replace(',', '', $vars); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.