how to get var name and value from function?
| From: | Afan Pasalic | Date: | Sat, 14 Apr 2007 00:49:12 +0000 |
| Subject: | how to get var name and value from function? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-252923@lists.php.net to get a copy of this message | ||
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.
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)
{
${$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?!?!?
:-(
any help appreciated.
-afan