I'm not even sure if what I am trying to do is possible, I have a simple php echo line as below..
<?php echo $T1R[0]['Site']; ?>
This works well but I want to make the "1" in the $T1R to be fluid, is it possible to do something like ..
<?php echo $T + '$row_ColNumC['ColNaumNo']' + R[0]['Site']; ?>
Where the 1 is replaced with the content of ColNaumNo i.e. the returned result might be..
<?php echo $T32R[0]['Site']; ?>
It is possible in PHP. The concept is called "variable variables".
The idea is simple: you generate the variable name you want to use and store it in another variable:
$name = 'T'.$row_ColNumC['ColNaumNo'].'R';
Pay attention to the string concatenation operator. PHP uses a dot (.
) for this, not the plus sign (+
).
If the value of $row_ColNumc['ColNaumNo']
is 32
then the value stored in variable $name
is 'T32R'
;
You can then prepend the variable $name
with an extra $
to use it as the name of another variable (indirection). The code echo($$name);
prints the content of variable $T32R
(if any).
If the variable $T32R
stores an array then the syntax $$name[0]
is ambiguous and the parser needs a hint to interpret it. It is well explained in the documentation page (of the variable variables):
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write
$$a[1]
then the parser needs to know if you meant to use$a[1]
as a variable, or if you wanted$$a
as the variable and then the[1]
index from that variable. The syntax for resolving this ambiguity is:${$a[1]}
for the first case and${$a}[1]
for the second.
You can do like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
$b = $$a;
echo "<pre>";
print_r($b[0]['Site']);
Or more simpler like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
echo "<pre>";
print_r(${$a}[0]['Site']);