UndeadCircus Posted July 30, 2011 Share Posted July 30, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/243255-storing-array-in-session-using-for-loop/ Share on other sites More sharing options...
trq Posted July 30, 2011 Share Posted July 30, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/243255-storing-array-in-session-using-for-loop/#findComment-1249325 Share on other sites More sharing options...
UndeadCircus Posted July 30, 2011 Author Share Posted July 30, 2011 HA! Wow.. I can't believe it was something that stupid. I was sitting on the couch with my fiance and staring at my code when it clicked in my head. Just as I was about to get back here and respond, I noticed your post. Thanks a lot for replying. Quote Link to comment https://forums.phpfreaks.com/topic/243255-storing-array-in-session-using-for-loop/#findComment-1249326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.