Hi all, i have a multidimentional array i want to pass as a session variable.
Of course i serialized the array.
I have 2 files, set.php & get.php
set.php populates the array, and get.php prints it out. below is my code:

Set.php:

<?php
session_start();
$array = $_SESSION['array'];

if ($array) {
    $associative = unserialize($array);
	print "Array Exists <br />";
} else {
    $associative = array();
	print "Array Created <br />";
}

$associative[0] = array();
$associative[1] = array();
$associative[2] = array();
$associative[0][0] = "0-0";
$associative[0][1] = "0-1";
$associative[1][0] = "1-0";
$associative[1][1] = "1-1";
$associative[2][0] = "2-0";
$associative[2][1] = "2-1";


$_SESSION['array'] = serialize($array)
?> 

Get.php:

<?php
session_start();
$array = $_SESSION['array'];

if ($array) {
    $associative = unserialize($array);
	print "Array Exists <br />";
} else {
    $associative = array();
	print "Array Created <br />";
}

for ($i = 0; $i < count ($associative); $i++){
    print "Associative array: ".$associative[$i];
	print "Subarray1: ".$associative[$i][0];
	print "Subarray2: ".$associative[$i][1];
}

?> 

So i navigate to set.php, then to get.php but nothing prints....
What am i doint wrong?

    To start with, you don't need to be serializing/unserializing anything: that's built into the session handler.

    Meanwhile in set.php you get $array from out of the session, create a new array $associative, and then put $array back into the session unchanged. Since you never put anything into the session in get.php either, there is nothing going on that puts anything into the session that isn't already there.

    So: drop the unnecessary serialization/unserialization business, and drop the extra fiddling around with that extra $array variable, too. $SESSION['array'] = $associative to store, and $associative = $SESSION['array'] to retrieve.

      PERFECT! Wery much appriciated.
      Thank you for your time, and the prompt response.
      ~Gabor

        Disregard my last post. I am still having problems....
        I am using this code, so each time i refresh the page a new value should be read...
        But its not 🙁

        <?php
        session_start();
        // id, and color_selection are passed via the $_GET method
        
        print "--- Debug info ---<br />";
        print "Passed color: ".$_GET['color_selection']."<br />";
        if ($_SESSION['color']) {$color = unserialize($_SESSION['color']); print "Color array loaded <br />";} else {$color = array(); print "Color array created <br />";}
        
        for ($i = 0; $i < count ($color); $i++) {
             print "<br />--- Reading Array <br /> Index: ".$i."<br />ID: ".$color[$i]["id"]."<br />Value: ".$color [$i]["value"]."<br />";
        }
        
        $arraySize = count ($color);
        $color[$arraySize] = array();
        $color[$arraySize]["id"] = $_GET['id'];
        $color[$arraySize]["value"] = $_GET['color_selection'];
        print "<br />--- Writing to Array --- <br />Index: ".$arraySize."<br />ID: ".$color[$arraySize]["id"]."<br />Value: ".$color [$arraySize]["value"]."<br />";
        
        $_SESSION['color'] = $color;
        
        ?> 

          Should you be getting rid of the unserialize() in the if condition, since you are no longer serializing it?

            :eek: Can't believe i didn't catch that...
            That worked....
            Guess it shows i haven't done php in a long time...
            Thank you so much

              Write a Reply...