Hi again,

I'm stumped. I'd like to store results from a questionnaire in a multi-dimensional session variable to tally the number of questions answered after the entire questionnaire is completed. Users choose selections from checkboxes, so I have that info coming from an array. I was hoping I could just create an array of answers, keyed by the question, but I'm having some issues. Here is a code snippet.

//items is the checkbox
//$curr is a string created designating the question
$answers = $_POST['items'];
if (count($answers) > 0)
{	
	$_SESSION['answers'][$curr]=$answers;	

	print_r($_SESSION['answers']);
}

When I print out the contents of the array, it gives me:
Array ( [0] => 31 [1] => 32 [2-2] => Array ( [0] => 31 [1] => 32 [2-2] => Array *RECURSION* ) )

I feel like it should be:
Array( [2-2] => Array ( [0] => 31 [1] => 32 )
At least that's what I want to happen. I also only have one php page and depending on what's passed into the url, different questions are shown. Because of this, it seems like my answers array isn't adding the new arrays.

Weird thing is when I hardcoded everything, this worked just fine. Now that I want it more dynamic, it isn't working. Any ideas? Or maybe there's some other way I'm not seeing. TIA!

Try the
print_r($_SESSION);
before you set to see how it looks.

This might help with the debugging.

<?
//items is the checkbox
//$curr is a string created designating the question
$answers = $_POST['items'];
if (count($answers) > 0)
{
	// Loop through the answers, and for each answer assign the value to $a
	foreach($answers as $a):
		// Then make an array within the answers and $curr array and store value of $a
		$_SESSION['answers'][$curr][]=$a;
	endforeach;	
	
	// Print_r to check array
	print_r($_SESSION['answers']);
}
?>

That should work - I have not tested it, but it should loop through each value from the checkboxes and put it into the array like you want.

This article has been dead for over six months. Start a new discussion instead.