dongye8110 2014-10-18 06:28
浏览 40

如果作为数组或对象传递,则PHP变量变量不显示

This works with simple variables. But it shows empty result with complex variables. AM I MISSING SOMETHING HERE? or is there anyother way around. Thanks.

#1. This works with simple variables.
$object = "fruit";
$fruit = "banana";

echo $$object;   // <------------ WORKS :outputs "banana".
echo "
";
echo ${"fruit"}; // <------------ This outputs "banana".


#2. With complex structure it doesn't. am I missing something here?
echo "
";
$result = array("node"=> (object)array("id"=>10, "home"=>"earth", ), "count"=>10, "and_so_on"=>true, );
#var_dump($result);

$path = "result['node']->id";
echo "
";
echo $$path; // <---------- This outputs to blank. Should output "10".
  • 写回答

2条回答 默认 最新

  • dragon8002 2014-10-18 07:38
    关注

    Not exactly using variable variables, but if you want to use a variable as the var name, eval should work

    $path = "result['node']->id"; eval("echo $".$path.";");

    评论

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退