syntax - PHP $$var['index'] = something; fails, although I thought it would work just like $$var = something; does -
i trying use $$ syntax in php accessing arrays can put name of variable inside variable , access variable.
i have used syntax many times before in different ways, surprise didn't work me , lost lot of time on it.
here sample code replicate problem:
$test=array( 'a'=>array(array(1,2,3),array(4,5,6),array(7,8,9)) ); $var = 'test'; var_dump($$var); var_dump($$var['a']);
the line var_dump($$var)
works expected, i'm getting warning: illegal string offset 'a' @ line var_dump($$var['a']);
, var_dump prints null
why doesn't work? doing wrong here? there work around if syntax not supported arrays?
your $$var['a']
equivalent ${$var['a']}
. not ${$var}['a']
. latter being workaround syntax looking for.
quoting php manual on variable variables:
in order use variable variables arrays, have resolve ambiguity problem. is, if write
$$a[1]
parser needs know if meant use$a[1]
variable, or if wanted$$a
variable ,[1]
index variable. syntax resolving ambiguity is:${$a[1]}
first case ,${$a}[1]
second.
Comments
Post a Comment