473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

referencing an array element with a string variable

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
4 1650

"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
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


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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: xcomm | last post by:
Hi All, <?php $vars= array("_SERVER","_SERVER","_SERVER","_SERVER","_SERVER","_SERVER"); foreach($vars as $var) { if(isset($$var))echo("$var: ${$var}<br>\n"); } ?> php.net:
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
12
by: flipflop | last post by:
I need to create a global array whose dimensions depend on the contents of another global array populated at its initialisation. For example: int array1={3,2,1}; int array2]; //should be...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
6
by: virgincita schmidtmann | last post by:
Good evening, I would like to pass the size of an array from the commandline. int main(int argc, int *argv) { .... max=*argv; int list; ....
5
by: nembo kid | last post by:
In the following function, s shouldn't be a pointer costant (array's name)? So why it is legal its increment? Thanks in advance. /* Code starts here */ void chartobyte (char *s) { while...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.