RE: [PHP] Dynamically named variables and arrays.
| From: | Ford, Mike [LSS] | Date: | Thu, 22 May 2003 09:39:57 +0000 |
| Subject: | RE: [PHP] Dynamically named variables and arrays. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-148522@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Chris Boget [mailto:chris@wild.net]
> Sent: 21 May 2003 16:46
>
> I know this works:
>
> $varName = 'bob';
> ${$varName} = 'joe';
>
> Now, what I'm hoping to to is take the same premis and apply it to
> arrays (particularly POST). What I'm doing is creating form element
> names as follows:
>
> sales_data[area][1]
> sales_data[area][2]
> ...
> sales_data[area][5]
>
> and so on. Upon form submission, we all know that PHP turns the
> above into an array that I can iterate through and get the
> values. Now,
> I now the names of the form elements. So using that, I'm trying to
> access the value of the outermost element dynamically. To do
> that, I'm
> doing:
>
> <script language="php">
>
> $requiredPostFieldName = str_replace( "sales_data", "",
> "sales_data[area][2]" );
> $fieldVal = ${"_POST['sales_data']{$requiredPostFieldName }"};
>
> </script>
>
> But that's not working. Should it? Am I doing something wrong? Any
> ideas or suggestions would be very much appreciated!
I can't seem to get my head round what you're trying to do from the above example, but by
any chance do you just want:
$_POST['sales_data']['area'][2]
?
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211