i have the following 3 variables:
private $a_total;
private $b_total;
private $c_total;
now when the fields are filled out with some data for totals, names of those fields are "a", "b", and "c". I want to store it dynamically to the variables above something like:
$type = $_POST['totaltype']; //either a, b or c
$to_save = "{$type}_total";
$this->$to_save['total'] = some number;
if i try to
print_r($this->$to_save);
it gives empty array. If i try to:
print_r($this->$to_save['total']);
it gives correct number.
can anyone help?
Note: i want to use dynamically because these data will be inside a big loop so i don't want to reuse $a_total
, $b_total
, $c_total
since i will have more than a
, b
and c
variables.
$this->totals[$type]
. There's no reason to dynamically create a variable for this case. – freshnode Nov 5 '12 at 14:01serialise($this->totals)
you'll get an array with an associative structure. If you're using one of the below answers, go for it - just didn't immediately see why you'd need to incur the overhead of dynamically creating these variables. – freshnode Nov 5 '12 at 14:07