【问题标题】:How to convert string (name of a variable) to a variable in PHP? [duplicate]如何将字符串(变量名)转换为 PHP 中的变量? [复制]
【发布时间】:2014-07-25 16:48:18
【问题描述】:

是否有可能使用字符串作为实际变量名? 像这样:

$a = '$myvariable[\'subitem\']';

如何转换变量 $a 以便实际使用变量

$myvariable['subitem'] ?

示例:

$myvariable['subitem'] = "hello";

$a = '$myvariable[\'subitem\']';

// do somenthing with $a

echo $a  // outputs "hello" instead of $myvariable['subitem']

【问题讨论】:

  • 你希望变量名是$myvariable['subitem']的值吗?
  • 使用$item = 'subitem'; $myarray[$item]有什么问题? (我不清楚目标。)

标签: php string variables


【解决方案1】:

这个怎么样:

$a = 'myvariable["subitem"]';
echo $$a;

上面叫variable variables

【讨论】:

  • 我认为你没有理解我的意思..
  • 你的意思是 $$a = "something new";
  • $a 应该只包含一个变量的名称..
  • 我的意思是使用字符串(代表变量的名称)来获取实际的变量..
  • @Totallama 像我描述的那样使用 $$a。试试我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
相关资源
最近更新 更多