Reply to Re: associative arrays as variable variables ($$)

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация