Page 1 of 1

Adding values to nested array

Posted: Mon Apr 14, 2008 9:48 am
by aceconcepts
Hi,

I am trying to add some values to a nested array via a foreach loop.

This is how I am trying to add my values:

Code: Select all

 
$_SESSION['selectedStageQuestions'][$expKey][$expStage][$expQuestionKey][]=$seqNumber;
 
However, the following error is thrown:

Fatal error: [] operator not supported for strings in...

I have been able to add values using the above method before but it just doesn't want to work this time.

The value I am adding is definitely a number!

Re: Adding values to nested array

Posted: Mon Apr 14, 2008 10:00 am
by onion2k
The problem is that $_SESSION['selectedStageQuestions'][$expKey][$expStage][$expQuestionKey] contains a string rather than an array. If you do..

Code: Select all

echo $_SESSION['selectedStageQuestions'][$expKey][$expStage][$expQuestionKey];
..you'll see a string rather than Array. At some point you've probably go $_SESSION['selectedStageQuestions'][$expKey][$expStage][$expQuestionKey] = ""; or $_SESSION['selectedStageQuestions'][$expKey][$expStage][$expQuestionKey] = $strVariable;.