变量可以访问数组值吗? [英] can array values be accessed by variable variables?

查看:59
本文介绍了变量可以访问数组值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只能通过可变变量正确访问的数组,如下所示:

I have an array which I can only access correctly via variable variables, like so:

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

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

echo $$fixed_name_variable;

在回声的pie中.除了只是不返回任何东西.因此,我需要知道这种方法是否切实可行,或者是否需要重新考虑.

Which in theroy echo's pie. Except it's just not returning anything. So I need to know if this approach is actually workable or if I need a rethink on it.

刚刚注意到.在第二行上,该栏应该用引号引起来吗?

Just noticed. On the second line, should the bar be in quotes?

推荐答案

尽管我讨厌鼓励这种行为,但是您可以使用eval在有限的范围内实现目标.

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"

这篇关于变量可以访问数组值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆