jeff5656 Posted November 18, 2012 Share Posted November 18, 2012 (edited) Let's say I have a bunch of records in an array and I want to echo one out: echo $somarray['somefield']; but lets say I want to have a variable instead of "somefield". How do I do this? This is what I want to do but the syntax is wrong: $hello = "somefield" echo $somarray['$hello']; Thanks. Edited November 18, 2012 by jeff5656 Quote Link to comment https://forums.phpfreaks.com/topic/270859-using-a-variable-in-an-array/ Share on other sites More sharing options...
us2rn4m2 Posted November 18, 2012 Share Posted November 18, 2012 <?php $somarray = array(); $somarray['someKey'] = 'someValue'; $key = 'someKey'; echo $somarray[$key]; // output someValue Quote Link to comment https://forums.phpfreaks.com/topic/270859-using-a-variable-in-an-array/#findComment-1393367 Share on other sites More sharing options...
Pikachu2000 Posted November 18, 2012 Share Posted November 18, 2012 In other words, the single quotes around the variable in the array index are causing the problem. Variables aren't interpolated in single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/270859-using-a-variable-in-an-array/#findComment-1393377 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.