【问题标题】:In Php, Calling an array when it's name is stored in a variable在 PHP 中,当数组的名称存储在变量中时调用数组
【发布时间】:2011-01-28 09:32:49
【问题描述】:

可能重复:
Convert a String to Variable

例如:

$test = array("this","is","a","test");

$what_array_to_read = "$test[0]";

读取数组的函数$test[0]

【问题讨论】:

  • 这与这个问题非常相似:stackoverflow.com/questions/433302/… 就个人而言,如果你需要这样做,恕我直言,你做错了什么。这有什么用例?
  • 也许你想把你的数组塞进一个哈希中。

标签: php arrays


【解决方案1】:

只有使用 eval 才能做到这一点,应该避免。

如果你这样做了

$what_array_to_read = "test";
$what_index_to_read = 0;

然后你就可以访问了

$$what_array_to_read[$what_index_to_read]; // will echo "this"

【讨论】:

    猜你喜欢
    • 2013-01-14
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多