Nested array problem

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

Nested array problem

Post by aceconcepts »

Hi,

I have a form which posts 2 fields as arrays.

Here are the fields as they appear in the form:

Code: Select all

 
<input type="text" name="seqQuestionNumber[]" class="numberInput" />
<input type="hidden" name="seqQuestionLocation[]" value="'.$key.'-'.$stage.'-'.$qKey.'" class="hidden" />
 
The value: value="'.$key.'-'.$stage.'-'.$qKey.'" refers to the "keys" of where the "seqQuestionNumber" is going to be inserted as a nested array.

I use the following to attempt to add each "seqQuestionNumber" as a nested array:

Code: Select all

 
for($x=0; $x < count($seqQuestionNumber); $x++)
{
    $seqArray[]=array($seqQuestionLocation[$x]=>$seqQuestionNumber[$x]);
}
                    
//ADD THE QUESTION NUMBERS TO THE CORRECT LOCATION IN $_SESSION['selectedStageQuestions']
                
//LOOP $seqArray
foreach($seqArray as $key=>$location)
{
    foreach($key as $qKey=>$number)
    {
        //GET EACH KEY FROM THE ARRAY ($key, $stageKey, $questionKey)
            $exp=explode("-", $location);
                            
                $expKey=$exp[0];
                $expStage=$exp[1];
                $expQuestionKey=$exp[2];
                                
        //ADD QUESTION NUMBERS TO $_SESSION['selectedStageQuestions']
            $_SESSION['selectedStageQuestions'][$expKey][$expStage][$expQuestionKey][]=array($expQuestionKey=>$number);
    }
}
 
When i run this script the following error is thrown: Fatal error: [] operator not supported for strings in...
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Nested array problem

Post by Zoxive »

Why so many Loops?

It looks like you need to rework how you store your results.
I have no idea what each part means, but I would most likely make the arrays like..

Code: Select all

$_SESSION['selectedStageQuestions'][] = array(
  'key'       =>  $Key,
  'stage'     =>  $Stage,
  'question'  =>  $Question,
);
Maybe even though the key in there (I don't know if its unique or not, maybe you could us something else)

Code: Select all

$_SESSION['selectedStageQuestions'][$Key] = array(
  'key'       =>  $Key,
  'stage'     =>  $Stage,
  'question'  =>  $Question,
);
Otherwise I would so something along the lines of..

Code: Select all

setStageQuestions($seqQuestionNumber,$seqQuestionLocation);
 
function setStageQuestions(array $Number, array $Location){
  $Num  = count($Number); 
  $Num2 = count($Location);
  if($Num != $Num2) return false;
  for($x=0;$x<$Num;$x++){
    $CurNumber = $Number[$x]; $CurLocation = $Location[$x];    
    list($Key,$Stage,$Question) = explode('-',$CurLocation);
    $_SESSION['selectedStageQuestions'][$Key][$Stage][$Question][] = array($Question,$CurNumber);
  }
  return true;
}
It would also help if we saw the full error.
Post Reply