Dynamic Variables
Posted: Sun May 17, 2015 7:18 pm
I have an array of objects -> $questions
Each object is an associative array, containing keys such as Answer1, Answer2, Answer3.. etc.
In order to get the answer for first question, I will access it as $questions[0]->Answer1
In my code, array index is dynamic and the answer number is dynamic something like this: $questions[$q]->Answer.$i
But I am not able to get the correct value with this.
I tried like this:
$q = 0;
$i = 1;
$temp = '$quesetions['.$q.']->Answer'.$i;
$value = ${$temp};
It throws the "Undefined Variable: $questions[0]->Answer1" error.
But If try to echo $questions[0]->Answer1, I get the correct value.
What is wrong in my code snippet above?
Thanks in advance.
Each object is an associative array, containing keys such as Answer1, Answer2, Answer3.. etc.
In order to get the answer for first question, I will access it as $questions[0]->Answer1
In my code, array index is dynamic and the answer number is dynamic something like this: $questions[$q]->Answer.$i
But I am not able to get the correct value with this.
I tried like this:
$q = 0;
$i = 1;
$temp = '$quesetions['.$q.']->Answer'.$i;
$value = ${$temp};
It throws the "Undefined Variable: $questions[0]->Answer1" error.
But If try to echo $questions[0]->Answer1, I get the correct value.
What is wrong in my code snippet above?
Thanks in advance.