jeff5656 Posted November 18, 2012 Share Posted November 18, 2012 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. 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 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. 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
Archived
This topic is now archived and is closed to further replies.