I have the main array called $quizzes
which contains the collection of $Quiz
. Each $Quiz
has the following fields: $Quiz['correct']
gives me the number of correct questions.
I can get the number of correct questions for 12th quiz using $quizzes[12]['correct']
However, since these quizzes are not displayed in order, I decided to define a new array:
$listoftests = array('$quizzes[30]','$quizzes[51]');
In my head, $listoftests[0]['correct']
should be equal to $quizzes[30]['correct']
but it's giving me
Warning: Illegal string offset 'correct' in /demo.php on line 14 $
when I try to echo $listoftests[0]['correct'];
In this $listoftests = array('$quizzes[30]','$quizzes[51]');
These are considered as two strings $quizzes[30]
and $quizzes[51]
. You should remove single quotes '
and try again.
Change this:
$listoftests = array('$quizzes[30]','$quizzes[51]');
To:
$listoftests = array($quizzes[30],$quizzes[51]);
By doing this
$listoftests = array('$quizzes[30]','$quizzes[51]');
you created array of 2 strings. That's it. Not an array of arrays.
You should remove quotes. And you can also use isset() to check if array item exists.
Remove the single quote and it shell work fine $listoftests = array($quizzes[30],$quizzes[51]);
@GRS you can do it in two way:
//case one where the element will be of string type
<?php
$quizzes[30] = 3;
$quizzes[51] = 32;
$listoftests = array("$quizzes[30]","$quizzes[51]");
var_dump($listoftests);
//or
//case two where the element will be of integer type
<?php
$quizzes[30] = 3;
$quizzes[51] = 32;
$listoftests = array($quizzes[30],$quizzes[51]);
var_dump($listoftests);