Using a string to reffer to an array or its member

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

Using a string to reffer to an array or its member

Post by Calimero »

Can this be done - and how ?

I have this string:

Code: Select all

$new_value = '$old_array[1][0]';
Now, I want to use it to reffer - call the value of that array-member.

Tried the eval(), but returned nothing.

Is there a way to do this ?

I need this string to generate endless ( how many levels are needed at that situation ) depth ( number of sub-levels ) od an array, by editing that string.


Hope I was clear enough.

Thanks Ahead !
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Using $$ can be used to refer to an variable..

Code: Select all

$test="Hello";
$vartest="test";
echo($$vartest);
would return "Hello". I seem to remember that there are problems with arrays...

You may want to look at Variable variables but it is not something I have ever played with.
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Re: Using a string to reffer to an array or its member

Post by SystemWisdom »

Calimero wrote:Can this be done - and how ?

I have this string:

Code: Select all

$new_value = '$old_array[1][0]';
[...]
Tried the eval(), but returned nothing.
[...]
The eval() function in PHP4 doesn't return the value of the evaluated string, it returns a flag indicating the evaluation succeeded/failed..

So, you could use eval() like this:

Code: Select all

eval( "\$new_value = \$old_array[1][0]" );
But Not like this:

Code: Select all

$new_value = eval( "\$old_array[1][0]" );
I hope that helps..
Post Reply