Current location: Hot Scripts Forums » Programming Languages » PHP » Variable variable/ generate an array


Variable variable/ generate an array

Reply
  #1 (permalink)  
Old 07-16-09, 12:44 PM
t_bandit t_bandit is offline
New Member
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Variable variable/ generate an array

for some reason or another I can seem to get this to work scoured the PHP manuel for hours ...
I looking for function to auto create variables from a HTML form using the name="" ($key) as the new $variable = " value="" ($value)"
what I have so far:
PHP Code:

foreach ($_REQUEST as $key => $value){ {$key". "=" . "$value";} 

For each input from the form Desired output :
$f_name = 'james';
$l_name = 'williams';
$phone = '5671230943';
etc...
Needed to utilize value later in script
Thanks for taking a look
Reply With Quote
  #2 (permalink)  
Old 07-16-09, 12:48 PM
t_bandit t_bandit is offline
New Member
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
missing code

actually this is what the php is I have so far
PHP Code:

foreach($_REQUEST){ ${$key"."="." $value ";} 

Reply With Quote
  #3 (permalink)  
Old 07-16-09, 03:25 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
You can use http://www.php.net/manual/en/function.extract.php:

Quote:
Warning
Do not use extract() on untrusted data, like user-input ($_GET, ...). If you do, for example, if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting extract_type values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.
PHP Code:

<?php

foreach ($_REQUEST as $k => $v)
{
        
$mVariable=$k;
        $
$mVariable=$v;
}

foreach (
$_REQUEST as $k => $v)
{
        if (isset($
$k))
                echo 
$k.' is variable '.$k.' and has value '.$$k.'<br />';
}

unset(
$a,$b,$c);

extract ($_REQUEST);

foreach (
$_REQUEST as $k => $v)
{
        if (isset($
$k))
                echo 
$k.' is variable '.$k.' and has value '.$$k.'<br />';
}
?>
Reply With Quote
Reply

Bookmarks

Tags
arrays, php, variables


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove value from array Nikas PHP 1 05-20-09 03:20 AM
Multi-Dimensional Array Help Nikas PHP 17 05-04-09 02:10 AM
How: Create an array with variable keys ginasuit PHP 2 02-20-09 08:14 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 08:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.