Array within a loop

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

Array within a loop

Post by aceconcepts »

Hi,

How can I add to an array without creating a nested array?

Here is my loop currently:

Code: Select all

 
for($x=0; $x<count($questionSelect); $x++)
{
    $questionRow=mysql_fetch_array(getQuestion($companyId, $questionSelect[$x]));
                                
    $_SESSION['selectedStageQuestionsText'][]=array($questionSelect[$x]=>$questionRow['strQuestionText']);
}
 
The above outputs the array like so: Array ( [0] => Array ( [59] => Will you be travelling with your partner? ) )

I basically want to get rid of the "holding array" Array([0]=>...) how can i add to the array without having to create as a nested array?

Thanks.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Array within a loop

Post by ghurtado »

I think you may want to change

Code: Select all

 
  $_SESSION['selectedStageQuestionsText'][]=array($questionSelect[$x]=>$questionRow['strQuestionText']);
 
to

Code: Select all

 
  $_SESSION['selectedStageQuestionsText'][$questionSelect[$x]] = $questionRow['strQuestionText'];
 
(if I understood your question correctly)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Array within a loop

Post by aceconcepts »

Nice one. That did the trick.
Post Reply