Jump to content
xisto Community

Getting An Array Value Of A Dynamic Variable

Recommended Posts

Ok i can't believe I need help in php but I can't figure this out... If i have an array, lets say called test, how do i get a certain value from that array when referencing it dynamically by either a string or another variable?


$test=array(1,2,5);$name="test";//Doesn't work:echo $$name[0];/Doesn't work:echo ${$name}[0];

Share this post

Link to post
Share on other sites
<?php$test=array(1,2,5);$name = &$test; // check this lineecho 'print_r $test';echo '<br />';echo '<pre>';print_r ($test);echo '</pre>';echo '<br />';echo 'print_r $test[0]';echo '<br />';print_r ($test[0]);echo '<br />';echo '<br />';echo 'print_r $name';echo '<pre>';print_r ($name);echo '</pre>';echo '<br />';echo 'print_r $name[0]';echo '<pre>';print_r ($name[0]);echo '</pre>';?>

Note line 2 and the prefix for references, compared to your code.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.