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?

Kieran
  • 69
  • 7
  • `$_SESSION['matresult[$i][$j']` ?????//// – M A SIDDIQUI Feb 23 '17 at 13:59
  • Possible duplicate of [Can I use array\_push on a SESSION array in php?](http://stackoverflow.com/questions/2616540/can-i-use-array-push-on-a-session-array-in-php) – Abdulla Nilam Feb 23 '17 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` :-) – Martin Feb 23 '17 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. – Guillermo Phillips Feb 23 '17 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. – Kieran Feb 23 '17 at 14:07
  • And, @Abdulla Nilam, I believe array_push would have been another method of achieving this so thank you, but I do not believe it is a duplicate at all. – Kieran Feb 23 '17 at 14:23

4 Answers4

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

?>
Alex
  • 436
  • 4
  • 9
  • what changes did you make? what did you fix? Edit your answer and improve it `:-)` – Martin Feb 23 '17 at 14:02
  • Thank you Alex, I noticed as with your original answer what you had altered prior to the EDIT. Such a simple fix, thank you. – Kieran Feb 23 '17 at 14:11
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'][][]....
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
bjorvack
  • 59
  • 2
  • 4
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'][][]....
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
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;
Guillermo Phillips
  • 2,176
  • 1
  • 23
  • 40