Re: how to get var name and value from function?
| From: | Tijnema ! | Date: | Sat, 14 Apr 2007 08:21:53 +0000 |
| Subject: | Re: how to get var name and value from function? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252928@lists.php.net to get a copy of this message | ||
On 4/14/07, Afan Pasalic <afan@afan.net> wrote:
hi, this one I can't figure out: I have to assign value of an array to variable named after key of the array several times in my project to , e.g. after I submit a form with personal info I have $_POST['name'] = 'john doe'; $_POST['address'] = '123 main st.'; $_POST['city'] = 'urbandale'; $_POST['zip'] = '12345'; $_POST['phone'] = '123-456-7980'; etc. Then I assign value to the var name: foreach ($_POST as $key => $value) { ${$key} = $value; } and then validate submitted.Are you sure you want to do this? You never know what a hacker inserts to your POST data, so he could easily define variables inside your script, especially when you're using more dangerous functions like system().
Though, to avoid writing all over again the same lines (even it's only 3 lines) I was thinking to create a function something like: function value2var($array, $print=0) { foreach ($_POST as $key => $value)I think you should change above line to : foreach ($array as $key => $value)
{
${$key} = $value;
echo ($print ==1) ? $key.': '.$value.'<br>'; // to test
results and seeing array variables and values
}
}
value2var($_POST, 1);
but, I don't know how to get info from function back to script?!?!?
:-(
Uhm, it's not even possible when you don't know the keys i believe.
Tijnema
any help appreciated. -afan