如何使用PHP构建动态变量? [英] How do I build a dynamic variable with PHP?

查看:149
本文介绍了如何使用PHP构建动态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PHP中构建一个动态变量,尽管在StackOverflow上已经有一些关于这个问题的问题,我仍然很难...:/



变量是我从来没有理解的东西 - 希望有人能指出我的方向正确。 :$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ >子节[$ {$ grandchild ['id']}] - > page [$ {$ greatgrandchild ['id']}] =蓬松的兔子;

显然,上述不起作用,但如果我硬编码一个变量:

  $ data ['query']  - > section [0]  - >小节[3]  - > page [6] =Very蓬松的兔子 

...然后一切都好,显然我没有正确构建动态变量。任何想法?



更新:



嗯,好的,我应该指出,这些不是一个数组 - 我使用指定为每个节点的属性的ID来处理XML中的节点,因此XML具有以下结构:

 code><字幕> 
< section id =0>
<子部分id =0>
< page id =1>我要写的内容< / page>
< page id =2>我要写的内容< / page>
< page id =3>我要写的内容< / page>
< / subsection>
< / section>
< / subtitles>

希望这有助于更好地解释事情。 :)

解决方案

为什么你认为你需要动态变量?这不是只是做你想要的:

  $ data ['query']  - > section [$ child [ id']]  - >子节[$ grandchild ['id']]  - > page [$ greatgrandchild ['id']] =蓬松的兔子; 


I'm trying to build a dynamic variable in PHP and despite looking at a number of the questions on the matter already here on StackOverflow I'm still stumped... :/

Variable variables is something I've never quite understood - hopefully someone here can point me in the right direction. :)

$data['query']->section[${$child['id']}]->subsection[${$grandchild['id']}]->page[${$greatgrandchild['id']}] = "Fluffy Rabbit";

Onviously the above does not work but if I hard code a variable as such:

$data['query']->section[0]->subsection[3]->page[6] = "Very Fluffy Rabbit";

...then all is fine, so obviously I'm not building my dynamic variable correctly. Any ideas?

UPDATE:

Hmm, ok I should have pointed out that these are not keys in an array - I'm addressing nodes in the XML using an ID which is specified as an attribute for each node, so the XML has the following structure:

<subtitles>
<section id="0">
<subsection id="0">
<page id="1">My content that I want to write</page>
<page id="2">My content that I want to write</page>
<page id="3">My content that I want to write</page>
</subsection>
</section>
</subtitles>

Hopefully that helps explain things a little better. :)

解决方案

Why do you think you need dynamic variables here? Doesn't this just do what you want:

$data['query']->section[$child['id']]->subsection[$grandchild['id']]->page[$greatgrandchild['id']] = "Fluffy Rabbit";

这篇关于如何使用PHP构建动态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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