can array values be accessed by variable variables?

不问归期 提交于 2019-12-02 09:36:41

Although I hate to encourage this behaviour, you can use eval to achieve what you to a limited extent.

$foo['bar'] = "pie";
$fixed_name_variable = "foo['bar']";

$a = eval("return $$fixed_name_variable;"); 
echo $a; //outputs "pie"

$foo[$key_var] should work, unless I misunderstood your question?

No, I don't think this is possible. The only thing (obviously) possible is to use a variable index, and access $foo[$bar].

However, using variable variables is usually very bad practice anyway - especially because they make debugging and automatic documentation / variable lookup so terribly difficult. It's usually best not to use them, but to use an array instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!