1

Right I had to make a variable as shown below.

$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';

But now the code wont run when shown anywhere it shows like $quizinfo['Q1_ID'] Instead of getting the variable that the php got earlier that behind it.

I need to be done like that as there 15 questions in a quiz and the ids I held and I change the number every time to get the new ID number but it not getting them how do I fix it?

checking it I put in.

echo $quizinfo['Q1_ID'];

And it worked correctly so what do I need to do to make it work?

karthikr
  • 97,368
  • 26
  • 197
  • 188
Ryan Bowden
  • 171
  • 1
  • 12

3 Answers3

7

Try:

$questionID = $quizinfo['Q' . $ques_num . '_ID'];

It should work.

When you write:

$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';

$quizinfo[…] is not interpreted. It is taken as a string.

See also:

Community
  • 1
  • 1
Jean
  • 7,623
  • 6
  • 43
  • 58
2

Since it is just php, why do you escape it?

$questionID = $quizinfo['Q' . $ques_num . '_ID'];

should do it. Also, You have ]' which should be replaced by '] at the end.

karthikr
  • 97,368
  • 26
  • 197
  • 188
1

I think you are looking to use a variable for your array index:

$index = "Q{$ques_num}_ID";
echo $quizinfo[$index];

The problem you're experiencing is that you've turned the whole expression into a string with a number inside it, and so no array look-up was being performed.

As an aside: having indices that are human-readable is nice for humans, but not necessary for a computer. Unless you have a particular need to do it this way, I'd change this so it is just numerically indexed - it'll simplify your code.

halfer
  • 19,824
  • 17
  • 99
  • 186