Dynamically named variables and arrays.
| From: | Chris Boget | Date: | Wed, 21 May 2003 15:45:56 +0000 |
| Subject: | Dynamically named variables and arrays. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-148424@lists.php.net to get a copy of this message | ||
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!
Christoph