0

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'];

1
  • what are you trying to there is n offset correct Commented Apr 11, 2017 at 10:54

4 Answers 4

1

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]);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

Remove the single quote and it shell work fine $listoftests = array($quizzes[30],$quizzes[51]);

Comments

1

@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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.