Renaming Variables in Foreach [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Renaming Variables in Foreach [Solved]

Post 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.
Last edited by MikeCXT on Tue Jan 17, 2006 12:56 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

${$variable1. $variable2} = $value;
its called variable variables
MikeCXT
Forum Newbie
Posts: 14
Joined: Fri Jan 13, 2006 1:26 pm

Post by MikeCXT »

Thanks.
Post Reply