niche Posted June 27, 2010 Share Posted June 27, 2010 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 More sharing options...
niche Posted June 27, 2010 Author Share Posted June 27, 2010 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 More sharing options...
Synook Posted June 27, 2010 Share Posted June 27, 2010 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 More sharing options...
jeffman Posted June 27, 2010 Share Posted June 27, 2010 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 More sharing options...
wirehopper Posted June 27, 2010 Share Posted June 27, 2010 <?php$b='b is 1';$vVariable='b';echo $$vVariable.PHP_EOL;$$vVariable='b is 2';echo $b.PHP_EOL; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.