Jump to content

simple dynamic variable question


niche

Recommended Posts

I need to create an array and name it dynamically.Here's the script I tried to use (unsuccessfully):

$counter = 1; $a.$counter = array(current($_POST));echo var_dump($a$counter);echo '</br>';echo var_dump($a1);echo '</br>';

This produces a parse error.Please, what am I doing wrong?Thanks

Link to comment
Share on other sites

I thing I get it! See: http://php.net/manual/en/language.variables.variable.phpAm this far:

$counter = 1; $a = 'a' . $counter;$$a = array(current($_POST));echo '</br>';echo var_dump($a1);

I'll check back if I get off the path.

Link to comment
Share on other sites

Creating variables like that is generally not the best option - have you considered using a multidimensional array?P.S. the break tag is empty, and is just written <br> (or <br /> in XHTML).

Link to comment
Share on other sites

Assuming you really do need a variable variable, that is the way to do it. As you discovered, variable identifiers cannot be concatenated like strings, because they are not really strings.The only 'exception' is array indexes, which can behave like strings. Example:$arr['a1'] = 'Hello';$ord1 = 'a';$ord2 = '1';echo $arr[$ord1 . $ord2]; // 'Hello'

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...