Page 1 of 1

Using a string to reffer to an array or its member

Posted: Wed Apr 13, 2005 2:53 am
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 !

Posted: Wed Apr 13, 2005 3:19 am
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.

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

Posted: Wed Apr 13, 2005 5:49 am
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..