Adding values to nested array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Adding values to nested array

Post 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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Adding values to nested array

Post 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;.
Post Reply