473,439 Members | 1,951 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,439 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 3505
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.