473,320 Members | 1,900 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,320 software developers and data experts.

array access via strings

Hi,

I would like to access an array via an index I have in a string. I know I
can pull the string apart and manually access it but that just looks ugly to
me :)

$pg['owner']['email'] = "fo*@bar.com";
$row->name = "[owner][name]"; //single quotes on 'owner' haven't helped
$value = "pg".$row->name;
echo $value; //ouput: 'pg[owner][email]'

echo $$value; //ouput: '' //not what i want!
eval ("\$value = $$value;");
echo $value; //output: 'fo*@bar.com' //what i want

Now i've tried lots of ${ } stuff but I'm at a loss - why does the eval ()
code work but not the real code!!!

Cheers
--
Kyle Maddison
km**********@cam.ac.uk
http://kyle.is-a-geek.com/
Jul 16 '05 #1
1 3499
Hi Kyle,

the "variable variable names" approach ($$value) doesnt work, because
"pg[owner][email]" is not a name for a variable that can be looked up.
The name of your variable is "pg".

You have to take your string that holds the keys apart:
for example:
$row->name="[owner]";
$row->mail="[email]";

echo $pg[$row->name][$row->mail];
would work.

That might imply that you have to split your string
$row->name = "[owner][name]";
to get the two parts, e.g. using preg_match.

eval works, because you sort of emulate variable variables
on a source-code level.

Cheers
Frank
Kyle Maddison wrote:
Hi,

I would like to access an array via an index I have in a string. I know I
can pull the string apart and manually access it but that just looks ugly
to me :)

$pg['owner']['email'] = "fo*@bar.com";
$row->name = "[owner][name]"; //single quotes on 'owner' haven't helped
$value = "pg".$row->name;
echo $value; //ouput: 'pg[owner][email]'

echo $$value; //ouput: '' //not what i want!
eval ("\$value = $$value;");
echo $value; //output: 'fo*@bar.com' //what i want

Now i've tried lots of ${ } stuff but I'm at a loss - why does the eval ()
code work but not the real code!!!

Cheers
--
Kyle Maddison
km**********@cam.ac.uk
http://kyle.is-a-geek.com/


Jul 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
13
by: jesper | last post by:
Hello, I have to load strings from a datbase and put them into an array. I get them by packets of 100 strings of 50 characters. Then I should do something like that : StringArray But x can be...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
by: John | last post by:
Hi I need to return an array of string in my own split function (access 97). I have defined the function as below but I get err on 'As String()'. What can I do to make the function return an...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
5
by: ramu | last post by:
Hi, Could anyone please tell me how to dereference a pointer to an array of pointers? Regards
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.