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:
[php]$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)[/php]
However, if I don't use the array it works:
[php]$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)[/php]
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?
I want to call several different variables depending on which cycle the for loop is in, so I have the following:
[php]$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)[/php]
However, if I don't use the array it works:
[php]$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)[/php]
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?
Comment