Variables inside variable names


$EuropeCountry=array("[COLOR="#FF0000"]Germany[/COLOR]");

$[COLOR="#FF0000"]Germany[/COLOR]_continent="Europe";

I have 2 variables like the above.

The value of the first variable “$EuropeCountry[0]” is “Germany”.
The value of the second variable “$Germany_continent” is “Europe”.

I like to replace “Germany” in the second variable name “$Germany_continent” with the first variable name “$EuropeCountry[0]”.

The trial code below is for it.

[b]trial code[/b]
$EuropeCountry=array("[COLOR="#FF0000"]Germany[/COLOR]");

$[COLOR="#0000CD"]($EuropeCountry[0][/COLOR])_continent="Europe";

[b]result[/b]
parse error

that gives a parse error. Try a multidimensional array instead

$world = array();
$world[] = array('country'=>'Germany', 'continent'=>'Europe');
$world[] = array('country'=>'USA', 'continent'=>'America');
$world[] = array('country'=>'Japan', 'continent'=>'Asia');
$world[] = array('country'=>'China', 'continent'=>'Asia');

or a simpler associative array

$world = array();
$world['Germany'] = 'Europe';
$world['USA'] = 'America';
$world['Japan'] ='Asia';
$world['China'] = 'Asia';

It does seem that maybe the structure of your code isn’t the best idea, though I couldn’t say for sure since I haven’t really seen any of it. So you may want to take captainccs’ thoughts and re-work your code some.
But to answer your question, there is a concept called “variable variables”


$EuropeCountry = array( 'Germany' );
$var_name = "{$EuropeCountry[ 0 ]}_continent"; // $var_name contains the string "Germany_continent"
${$var_name} = 'Europe'; // we're assigning a value to a variable named "Germany_continent"

echo $Germany_continent; // will print "Europe"

Here are a few examples of variable variables:

${$EuropeCountry[0] . '_continent'} = 'Europe';
// or
$variable_name = $EuropeCountry[0] . '_continent';
$$variable_name = 'Europe';

See http://php.net/variables.variable