Posted by Janwillem Borleffs on 07/22/05 13:33
evanescent.lurker@gmail.com wrote:
> $data[ 'Objects' ][ 'Spheres' ] = 'blah';
> $dataVar = "\$data[ 'Objects' ][ 'Spheres' ]";
> echo 'variable variable: ' . $$dataVar . '<br />';
> echo 'variable name: ' . $data[ 'Objects' ][ 'Spheres' ] . '<br />';
>
> I don't know why the 3rd line doesn't output what the 4th (last line)
> does...
>
Using a scalar:
$foo = 'bar';
$variable = 'foo';
print $$variable; // Prints 'bar'
Now, let's use an array:
$foo = array('bar' => 'foobar');
$variable = 'foo';
print ${$variable}['bar']; // Prints 'foobar'
Note the difference in the syntax. Next using your array:
$data[ 'Objects' ][ 'Spheres' ] = 'blah';
$dataVar = "data";
echo 'variable variable: ' . ${$dataVar}['Objects']['Spheres'] . '<br />';
echo 'variable name: ' . $data[ 'Objects' ][ 'Spheres' ] . '<br />';
Why this is? 'data' is the variable name, while Objects and Spheres are only
part of the structure of the $data variable.
JW
Navigation:
[Reply to this message]
|