Page 1 of 1

Renaming Variables in Foreach [Solved]

Posted: Tue Jan 17, 2006 11:16 am
by MikeCXT
I have a form that posts the data, which the next page gathers. The variables are never redeclared, so they remain the exact names from the form. I wanted to see if I could change the variable using a foreach statement on the _POST array. Something such as adding a number to the end of each variable name. For instance, chaning $name to $name1 and $email to $email1. I attempted this and got nothing but errors in my syntax, and can't find any examples online or in my books about changing a variable name like this. I was using this code:

Code: Select all

function rename_post() {
   foreach ($_POST as $key => $value) {
     $_POST[$key] = $value;
     $valueEXTRA = $value;
   }
}
Note the varaible $valueEXTRA is what I am trying to change to $value1. I do not actually use the EXTRA part, thats to make it easy to see what part I am talking about. I can't figure out add a number to the end, or if it is even possible.

I'm not even sure the $_POST[$key] = $value; is nessessary, but I was going to test that after I got the renaming part functioning.

Posted: Tue Jan 17, 2006 11:27 am
by John Cartwright

Code: Select all

${$variable1. $variable2} = $value;
its called variable variables

Posted: Tue Jan 17, 2006 12:58 pm
by MikeCXT
Thanks.