note 38724 added to language.types.array
| From: | Otter at rn2 dot php dot net | Date: | Sun, 04 Jan 2004 23:38:06 +0000 |
| Subject: | note 38724 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62912@lists.php.net to get a copy of this message | ||
Kieran wrote:
I ran across something that should be noted...
Take care when using variables for referencing arrays. PHP will interpret the square bracket syntax
[] as a substr() like reference....
<?
// this doesn't work, PHP evaluates $($foo[$bar]) instead of ($$foo)[$bar]
$temp = $$foo[$bar];
/*--------------------------------------------------------*/
// use this method instead:
// make a temp array so we can refer to it by an absolute name
$temp_array = $$foo;
// now pull the value
$temp = $temp_array[$bar];
?>
Cheers!
Kieran
You may also do this:
<?
$temp = ($$foo)[$bar];
//Or...
$temp = $($foo[$bar]);
?>
I forget where I read this... somewhere in documentation
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38724
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38724&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38724&report=yes
Search -- http://master.php.net/manage/user-notes.php