How do I build a dynamic variable with PHP?

佐手、 提交于 2019-12-23 17:05:05

问题


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. :)


回答1:


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";



回答2:


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:

$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




回答3:


$foo = "hello";

$$foo = " world";

//echo $foo.$$foo;

echo $foo.$hello;



回答4:


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.




回答5:


$foo='bobo';
echo $foo;//"bobo"
$$foo='koko';
echo $$foo;//"koko"
echo $bobo;//"koko"


来源:https://stackoverflow.com/questions/4077919/how-do-i-build-a-dynamic-variable-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!