bytte Posted October 28, 2008 Share Posted October 28, 2008 I have an array called "foo" that I want to echo to the screen. However, the array name "foo" is dynamically generated. So I can't write this in my script: echo $foo[0]; I first have to do some logic do get the variable name: //$someOtherVar contents is the string "foo" $varName = $someOtherVar; If $varname is now containing the string "foo", then how can I get the contents of the array with the name "foo"? I can't do this, can I? echo $varName[0]; I hope my question is clear. Link to comment https://forums.phpfreaks.com/topic/130417-simple-question-about-variables/ Share on other sites More sharing options...
rhodesa Posted October 28, 2008 Share Posted October 28, 2008 no problem, dynamic variables (aka variable variables): <?php $foo = array( 1, 2, 3 ); $varName = 'foo'; echo ${$varName}[0]; ?> Link to comment https://forums.phpfreaks.com/topic/130417-simple-question-about-variables/#findComment-676507 Share on other sites More sharing options...
GKWelding Posted October 28, 2008 Share Posted October 28, 2008 EDIT: Nevermind, talking crap as usual, just ignore me... Link to comment https://forums.phpfreaks.com/topic/130417-simple-question-about-variables/#findComment-676510 Share on other sites More sharing options...
bytte Posted October 28, 2008 Author Share Posted October 28, 2008 Thanks a lot. Very helpful. Link to comment https://forums.phpfreaks.com/topic/130417-simple-question-about-variables/#findComment-676608 Share on other sites More sharing options...
revraz Posted October 28, 2008 Share Posted October 28, 2008 Is this a homework assignment? Link to comment https://forums.phpfreaks.com/topic/130417-simple-question-about-variables/#findComment-676626 Share on other sites More sharing options...
bytte Posted October 28, 2008 Author Share Posted October 28, 2008 Yeah, the part you failed. Link to comment https://forums.phpfreaks.com/topic/130417-simple-question-about-variables/#findComment-676682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.