joe92 Posted November 5, 2011 Share Posted November 5, 2011 $word = 'numbers'; $numbers= array('1', '2', '3', '4'); echo $$word[0]; I expected the output to be '1'. It ended up being nothing... :-\ Why does this not work? Is it not possible to have a variable variable array? And if not, is there a workaround? Cheers, Joe Quote Link to comment https://forums.phpfreaks.com/topic/250525-variable-variable-array/ Share on other sites More sharing options...
xyph Posted November 5, 2011 Share Posted November 5, 2011 The square brackets can also return a given character in a string. <?php $word = 'numbers'; echo $word[0]; ?> will output 'n' What you want to do is use curly braces to let PHP know that you want the $word variable isolated from the $ at the start, and the [0] at the end. <?php $word = 'numbers'; $numbers= array('1', '2', '3', '4'); echo ${$word}[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250525-variable-variable-array/#findComment-1285325 Share on other sites More sharing options...
joe92 Posted November 5, 2011 Author Share Posted November 5, 2011 Works like a charm. Thank you xyph! Quote Link to comment https://forums.phpfreaks.com/topic/250525-variable-variable-array/#findComment-1285336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.