[英]How do I build a dynamic variable with PHP?
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... :/ 我正在尝试在PHP中构建一个动态变量,尽管在StackOverflow上已经有关于此问题的一些问题,我仍然难过......:/
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: 嗯,好吧我应该指出这些不是数组中的键 - 我使用ID指定XML中的节点,该ID被指定为每个节点的属性,因此XML具有以下结构:
<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";
$foo = "hello";
$$foo = " world";
//echo $foo.$$foo;
echo $foo.$hello;
In this example you don't need dynamic variables. 在此示例中,您不需要动态变量。
If $child["id"] has the value 0, $grandchild["id"] has the values 3 and $greatgrandchild["id"] has the value 6, you should use something like: 如果$ child [“id”]的值为0,$ grandchild [“id”]的值为3且$ greatgrandchild [“id”]的值为6,则应使用以下内容:
$data['query']->section[$child['id']]->subsection[$grandchild['id']]->page[$greatgrandchild['id']] = "Fluffy Rabbit";
Normally you use dynamic variables like this: 通常你使用这样的动态变量:
$variable = "variableName";
$$variable = "Some value";
echo $variableName;
This will display: 这将显示:
Some value
EDIT 编辑
Totally agree with ircmaxell 完全同意ircmaxell
I looks like you're confusing variable variables with good old array keys. 我看起来你把变量变量与旧的数组键混淆了。 Variable variables are a mechanism that allows to read (or write) a value into a variable whose name is unknown or can change and, honestly, they're hardly ever necessary: 变量变量是一种允许将值读取(或写入)名称未知或可以更改的变量的机制,老实说,它们几乎不需要:
<?php
$first_name = 'John';
$last_name = 'Smith';
$display = 'first_name';
echo $$display; // Prints 'John';
$display = 'last_name';
echo $$display; // Prints 'Smith';
However, your code suggest that you only want to access a key inside an array: 但是,您的代码建议您只想访问数组中的键:
<?php
$person = array(
'first_name' => 'John',
'last_name' => 'Smith',
);
$display = 'first_name';
echo $person[$display]; // Prints 'John';
$display = 'last_name';
echo $person[$display]; // Prints 'Smith';
In PHP, an array key is either an integer or a string, but it doesn't need to be a literal: you can retrieve the key from a variable. 在PHP中,数组键是整数或字符串,但它不需要是文字:您可以从变量中检索键。
$foo='bobo';
echo $foo;//"bobo"
$$foo='koko';
echo $$foo;//"koko"
echo $bobo;//"koko"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.