Variables Variable Question
Question posted by: TheServant
(Needs Regular Fix)
on
May 18th, 2008 01:05 PM
As you might have guessed from some of my questions today, I am trying to automate a lot of my pages with for loops. However I have become stuck, yet again!
I want to call several different variables depending on which cycle the for loop is in, so I have the following:
Code: ( text )
$var_data_name = "var_data['cost_".$i."']"; echo($var_data_name."<br />"); // prints: var_data['cost_1'] echo($$var_data_name."<br />"); // prints: {nothing} echo($var_data['cost_1']."<br />"); // prints: 1000 (value of cost_1)
However, if I don't use the array it works:
Code: ( text )
$bar_cost_1=var_data['cost_1']; $var_data_name = "bar_cost_".$i; echo($var_data_name."<br />"); // prints: bar_cost_1 echo($$var_data_name."<br />"); // prints: 1000 echo($var_data['cost_1']."<br />"); // prints: 1000 (value of cost_1)
So I know the problem is using an array in the vars var, but does anyone know how to get around it? I am happy to do it this way, but I was wondering if there is simple syntax which needed specifically for arrays or something?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
May 18th, 2008 01:23 PM
# 2
|
Re: Variables Variable Question
Actually, I have found a better way to do it, but I am interested to find it out anyway for future reference if anyone knows the answer!
|
|
May 18th, 2008 02:11 PM
# 3
|
Re: Variables Variable Question
See also chapter Variables variable in the PHP documentation. This handles the ambiguity of using indexes in var-vars. When I need to use it I use the eval() function. Like this
Code: ( text )
<?php $var_data['cost_1']=1000; $i=1; $var_data_name = "var_data['cost_".$i."']"; echo("1. ".$var_data_name."<br />"); // prints: 1. var_data['cost_1'] echo("2. ".$$var_data_name."<br />"); // prints: 2. {nothing} echo "3. ".eval("return \$$var_data_name;")."<br>"; // prints 3. 1000 echo("4. ".$var_data['cost_1']."<br />"); // prints: 4. 1000 (value of cost_1) ?>
Ronald
__________________
RTFM is an almost extinct art form.
|
|
May 18th, 2008 10:01 PM
# 4
|
Re: Variables Variable Question
Try enclosing the sub-variable in squigly brackets:
Code: ( text )
echo($$var_data_name."<br />"); // prints: {nothing}
becomes
Code: ( text )
echo(${$var_data_name}."<br />");
|
|
May 18th, 2008 10:04 PM
# 5
|
Re: Variables Variable Question
Great solutions, I will give that a go as soon as I get home.
|
|
May 19th, 2008 03:57 AM
# 6
|
Re: Variables Variable Question
Ron,
echo (eval("return \$$race_data_name;")."<br />"); works perfectly! I will change my script to this for now.
aktar,
Your's in the one I have been trying to do for ages, but I can't get it to work? Have you tried your suggestion? If I can get it to work with the same logic as yours then that will be my solution.
|
|
May 19th, 2008 04:29 PM
# 7
|
Re: Variables Variable Question
aktar's solution does not work for me either. And I am not quite sure why it should work, reading the variables-variable chapter in the PHP documentation in the link I previously posted.
Ronald
__________________
RTFM is an almost extinct art form.
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Top PHP Forum Contributors
|