Re: Variable array variables?
| From: | Matt McClanahan | Date: | Sun, 15 Oct 2000 01:59:14 +0000 |
| Subject: | Re: Variable array variables? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-20188@lists.php.net to get a copy of this message | ||
On Sat, 14 Oct 2000, Lars Torben Wilson wrote:
> I've been searching archives and the 'net all day--and my memory, but
> that seems to be down at the moment--for this. Given an array like:
>
> $foo = array (
> 'bar' => array (
> 'name' => 'Bill'
> )
> );
>
> ...and strings like $arr = 'foo' and $ind = '[bar][name]' (format can
> be changed if needed), what would be the best way of doing something
> like:
>
> echo ${$arr . $ind};
>
> I always get 'Undefined variable: foo[bar][name]...'
I'm not getting an undefined variable error, but I may just not have my
error reporting set high enough.
> This is PHP 4.0.4-dev, CVS from about 20 minutes ago.
Has this worked as you described in prior versions?
> Is this simply beyond the parser's capabilities, or am I merely
> missing an obvious syntax problem here? Either way, the answer will
> make it into the manual. :)
Here's what I came up with, running 4.0.1pl2..
$foo = array(
'bar' => array(
'name' => 'Bill'
)
);
$arr = 'foo';
$ind = '[bar][name]';
// This is undefined
echo ${$arr . $ind};
${$arr . $ind} = 'Another value';
// Now it's defined
echo ${$arr . $ind};
So does this mean I just created a string variable named $foo[bar][name]
If I do a
foreach ($GLOBALS as $key => $val)
echo "$key\n"
foo[bar][name] does indeed show up. I wonder if that should be allowed?
Matt