3

I tried storing data to php array from mysql using under code. But it's not working. This code:

echo $answerArray[count][i];

shows the correct result. But this code:

echo $answerArray[0][0];

doesn´t show anything.

What should I do to fix it? Thank you.

Full code:

   $count = 0; //answer count
   $answerArray = array();
    while ($row = mysqli_fetch_array($resultFromR, MYSQLI_ASSOC)) { //add array from db
     for($i = 0; $i < $questionNumber; $i++) {
     $j = $i + 1;
     $answerArray[count][i] = $row["num$j"];
     echo $answerArray[count][i]; //is working.
     }
$count++;
}
echo '<br />';
echo $answerArray[0][0]; //something wrong!!! I cannot get anything from this.
2
  • 1
    Have you tried print_r($answerArray); ? Commented Oct 9, 2011 at 7:21
  • 2
    $answerArray[count][i] ?? Should be $answerArray[$count][$i] for a starter Commented Oct 9, 2011 at 7:24

2 Answers 2

2

maybe this will work

 $answerArray[$count][$i] = $row["num$j"];

(add $ before count and i)

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure but shouldn't the [i] be [$i]?

3 Comments

yes, I think about this too. But he/she says that [i] is working. And it cannot work. AFAIK.
I believe that if your error reporting is set to none, $array[i] is equal to $array['i'] so, this is why it works when echo $array[i] afterwards. But echo $array[0] will not, even if $i = 0, and all that without a single warning
$foo[i] is equivalent to $foo['i'] if i is not a defined constant, so reading $answerArray[count][i] should work, I assume that is what he means by working and [0][0] not working

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.