0

So I have the following PHP script to add to random matrices together in order to create a dynamic type of question for a Quiz:

<?php
      $min = 0;
      $max = 10;

      $a1 = rand($min, $max);
      $b1 = rand($min, $max);
      $c1 = rand($min, $max);
      $d1 = rand($min, $max);

      $a2 = rand($min, $max);
      $b2 = rand($min, $max);
      $c2 = rand($min, $max);
      $d2 = rand($min, $max);

      $matrixa = array(
          array($a1,$b1),
          array($c1, $d1)
      );
      $matrixb = array(
          array($a2,$b2),
          array($c2, $d2)
      );                      

      for ($i=0; $i<2; $i++){
          for ($j=0; $j<2; $j++){
               $matresult[$i][$j] = $matrixa[$i][$j] + $matrixb[$i][$j];
               echo $matresult[$i][$j] . ' ';     
          }
          echo '<br>';
      }  
var_dump($matresult);
?>

This works, and stores the values correctly so that the output is as follows:

16 4
4 8
array(2) { [0]=> array(2) { [0]=> int(16) [1]=> int(4) } [1]=> array(2) { [0]=> int(4) [1]=> int(8) } } 

(For example)

Now, when I try to use a session variable within the same for loop:

for ($i=0; $i<2; $i++){
    for ($j=0; $j<2; $j++){
        $_SESSION['matresult[$i][$j'] = $matrixa[$i][$j] + $matrixb[$i][$j]; 
        echo $_SESSION['matresult[$i][$j]'] . ' ';
    }
    echo '<br>';
}  
var_dump($_SESSION['matresult']);

The output gives the following:

16 4
4 8
NULL

I don't understand why this is happening, the code and logic is exactly the same, what have I missed?

6
  • $_SESSION['matresult[$i][$j'] ?????//// Commented Feb 23, 2017 at 13:59
  • Possible duplicate of Can I use array_push on a SESSION array in php? Commented Feb 23, 2017 at 14:00
  • You have a typo. $_SESSION['matresult[$i][$j'] is wrong, you need to close that final array key ]. Also, mt_rand is more random than rand :-) Commented Feb 23, 2017 at 14:00
  • You should use double quotes, so that you get variable interpolation in your string. You are also missing a closing square bracket. Commented Feb 23, 2017 at 14:02
  • apologies for the minor syntax error, this was a typo when creating the question, the code within my project is syntax free. Appreciate the headsup though. Commented Feb 23, 2017 at 14:07

4 Answers 4

3

I changed

    $_SESSION['matresult[$i][$j'] = $matrixa[$i][$j] + $matrixb[$i][$j]; 
    echo $_SESSION['matresult[$i][$j]'] . ' ';

to

    $_SESSION['matresult'][$i][$j] = $matrixa[$i][$j] + $matrixb[$i][$j]; 
    echo $_SESSION['matresult'][$i][$j] . ' ';

This code should work as espected:

<?php

   ....

    for ($i=0; $i<2; $i++){
        for ($j=0; $j<2; $j++){
            $_SESSION['matresult'][$i][$j] = $matrixa[$i][$j] + $matrixb[$i][$j]; 
            echo $_SESSION['matresult'][$i][$j] . ' ';
        }
        echo '<br>';
    }  
    var_dump($_SESSION['matresult']);

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

2 Comments

what changes did you make? what did you fix? Edit your answer and improve it :-)
Thank you Alex, I noticed as with your original answer what you had altered prior to the EDIT. Such a simple fix, thank you.
1

Make sure that your session has a valid key

for ($i=0; $i<2; $i++){
    for ($j=0; $j<2; $j++){
        $_SESSION['matresult'][$i][$j] = $matrixa[$i][$j] + $matrixb[$i][$j]; 
        echo $_SESSION['matresult'][$i][$j] . ' ';
    }
    echo '<br>';
}  
var_dump($_SESSION['matresult']);

this is main index so when you want to add more then define like this

$_SESSION['matresult'][][]....

Comments

0

try this code

for ($i=0; $i<2; $i++){
for ($j=0; $j<2; $j++){
    $_SESSION['matresult'][$i][$j] = $matrixa[$i][$j] + $matrixb[$i][$j]; 
    echo $_SESSION['matresult'][$i][$j] . ' ';
}
echo '<br>';
 }  
var_dump($_SESSION['matresult']);

this is main index so when you want to add more then define like this

$_SESSION['matresult'][][]....

4 Comments

what changes did you make? what did you fix?
it was mistake , i want to change my answer . sorry
please check my update code . it was define which need to update
when i try to update my code but any how update other answer so why this is happen. answer is update and which was wrong also in this question
0

You could just put your array into the session outside the loop:

  for ($i=0; $i<2; $i++){
      for ($j=0; $j<2; $j++){
           $matresult[$i][$j] = $matrixa[$i][$j] + $matrixb[$i][$j];
           echo $matresult[$i][$j] . ' ';     
      }
      echo '<br>';
  }

  $_SESSION['matresult'] = $matresult;

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.