Adaaam Posted July 15, 2015 Share Posted July 15, 2015 Hello, Well this is embarrasing. Can anyone tell me why the $_SESSION['values'] array keeps getting overwritten every time I try adding a new value to it? Instead of the new value being added to the end of the array, the whole array gets overwritten with the new value and I don't know why? <?php session_start(); $_SESSION['values'] = array(); if (isset($_POST['value'])) { $_SESSION['values'][] = $_POST['value']; } var_dump($_SESSION['values']) . '<br>'; ?> <html> <head> <title>Test</title> </head> <body> <form method="post"> <input type="text" name="value"> <input type="submit" name="submit" value="submit"> </form> </body> </html> Hope this makes sense, I really don't know why it's not working. Thanks, Adam Link to comment https://forums.phpfreaks.com/topic/297317-php-sessions-array/ Share on other sites More sharing options...
fastsol Posted July 15, 2015 Share Posted July 15, 2015 Simple, you're clearing it on the 3rd line of the script everytime the page loads $_SESSION['values'] = array(); This would be more appropriate. if(!isset($_SESSION['values'])) { $_SESSION['values'] = array(); } Link to comment https://forums.phpfreaks.com/topic/297317-php-sessions-array/#findComment-1516503 Share on other sites More sharing options...
Adaaam Posted July 15, 2015 Author Share Posted July 15, 2015 I knew it would be something stupid. Thanks for pointing that out. Link to comment https://forums.phpfreaks.com/topic/297317-php-sessions-array/#findComment-1516504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.