Jump to content

Storing array in session using for loop


UndeadCircus

Recommended Posts

Hey everyone,

 

Got an interesting problem here that's driving me nuts. What I'm trying to do is store an array in a session variable. I'm trying to do this with a for loop. This is what I have right now:

 

session_start();

$frmTourDates = 1500;
for($i=0;$i<5;$i++){
$_SESSION['tourDates'] = array();
$_SESSION['tourDates'][$i] = $frmTourDates;
$frmTourDates += 500;
}

print_r($_SESSION['tourDates']);

 

But for some reason, this is only giving me one value in the array with I use the print_r command. The output is:

 

Array ( [4] => 3500 )

 

Is there some reason it won't give me [0] => 1500, [1] => 2000, [2] => 2500, [3] => 3000, [4] => 3500?

 

I would think that using $i inside of $_SESSION['tourDates'][$i] would pretty much say $_SESSION['tourDates'][0], $_SESSION['tourDates'][1], $_SESSION['tourDates'][2], $_SESSION['tourDates'][3], $_SESSION['tourDates'][4] since the $i variable is increasing by one each time.

 

I'm racking my brain with this... Hopefully someone can help!

 

Thanks!

Link to comment
Share on other sites

Your resetting $+SESSION['tourDates'] to an empty array within each iteration of the loop.

 

$frmTourDates = 1500;
$_SESSION['tourDates'] = array();
for($i=0;$i<5;$i++){
$_SESSION['tourDates'][$i] = $frmTourDates;
$frmTourDates += 500;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.