By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

referencing an array element with a string variable

P: n/a
I've been researching this for hours and can't seem to find the right
syntax.

I need to retrieve a value of an array by referencing the element using a
string variable.

For example:

$data['lastname'] = 'hank';
$element = 'lastname';
echo $data[$element];

Outputs:

hank

Seems straightforward enough, but the solution escapes me.

Someone clue me in--thanks!


Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Bosconian" <bo*******@planetx.com> wrote in message
news:97********************@comcast.com...
I've been researching this for hours and can't seem to find the right
syntax.

I need to retrieve a value of an array by referencing the element using a
string variable.

For example:

$data['lastname'] = 'hank';
$element = 'lastname';
echo $data[$element];

Outputs:

hank

Seems straightforward enough, but the solution escapes me.

Someone clue me in--thanks!


echo $data["$element"];

Shelly
Jul 19 '05 #2

P: n/a
Both Shelly's and yours output the same answer -- which seems to be the
answer you're looking for.

What is the problem?

Jul 21 '05 #3

P: n/a


Bosconian wrote:
I've been researching this for hours and can't seem to find the right
syntax.

I need to retrieve a value of an array by referencing the element using a
string variable.

For example:

$data['lastname'] = 'hank';
$element = 'lastname';
echo $data[$element];

Outputs:

hank

Seems straightforward enough, but the solution escapes me.


That should work fine. What problems are you encountering? What are you
trying to do?

Ken

Jul 21 '05 #4

P: n/a
"Ken Robinson" <ke******@rbnsn.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...


Bosconian wrote:
I've been researching this for hours and can't seem to find the right
syntax.

I need to retrieve a value of an array by referencing the element using a string variable.

For example:

$data['lastname'] = 'hank';
$element = 'lastname';
echo $data[$element];

Outputs:

hank

Seems straightforward enough, but the solution escapes me.


That should work fine. What problems are you encountering? What are you
trying to do?

Ken


Thanks to all who responded.

Turns out the syntax was correct (as noted.) My array was in fact not being
defined correctly so the element was therefore empty.

Sorry for wasting your time.
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.