ridiculous Posted January 25, 2008 Share Posted January 25, 2008 I have simple question that I haven't been able to source an answer for. Hopefully, someone can "word all over this piece." /// Here's my associative array: $girl['erica']="fat"; $girl['monica]="pregnant"; $girl['vanna']="old"; /// Here's a variable that I want to use in place of hardcoding in a key $var = "erica"; //// Here's how I am unsuccessfully trying to print the key, value pair print ($girl[$var]); //// The result I am looking for, of course, is equivalent to $girl['erica']="fat"; Quote Link to comment https://forums.phpfreaks.com/topic/87720-what-is-the-correct-syntax-to-make-array-key-a-variable/ Share on other sites More sharing options...
rajesh Posted January 25, 2008 Share Posted January 25, 2008 Try the below code <?php /// Here's my associative array: $girl['erica']="fat"; $girl['monica']="pregnant"; $girl['vanna']="old"; /// Here's a variable that I want to use in place of hardcoding in a key $var = "erica"; //// Here's how I am unsuccessfully trying to print the key, value pair print ($girl["$var"]); //// The result I am looking for, of course, is equivalent to $girl['erica']="fat"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/87720-what-is-the-correct-syntax-to-make-array-key-a-variable/#findComment-448670 Share on other sites More sharing options...
p2grace Posted January 25, 2008 Share Posted January 25, 2008 I just tested the original code and it worked perfectly on my box. What exactly is it displaying that's not right? Quote Link to comment https://forums.phpfreaks.com/topic/87720-what-is-the-correct-syntax-to-make-array-key-a-variable/#findComment-448671 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.