Scooby08 Posted February 19, 2010 Share Posted February 19, 2010 I'm trying to store an array of checkbox values into a session array, sort of like a shopping cart, but I can't figure out how to not overwrite the sessions array each time new checkbox values are added to the stored session... Here is an array that is created upon submitting with two checkboxes checked: Array ( [0] => 606613 [1] => 606614 ) And then my sessions array is now like so: Array ( [checked] => Array ( [0] => 606613 [1] => 606614 ) ) The code for that so far is this: <?php if (!isset($_SESSION['checked'])) { $_SESSION['checked'] = array(); } $_SESSION['checked'] = $_GET['new_checked']; ?> I know I probably need to add a loop that checks the arrays, but I just can't think how I should go about adding newly checked checkbox values, that are not duplicates, to the *end* of the session array "checked"?? Would anybody care to share some ideas? THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/192592-how-to-store-checkbox-array-in-a-session-and-then-be-able-to-add-to-it/ Share on other sites More sharing options...
sasa Posted February 19, 2010 Share Posted February 19, 2010 try <?php if (!isset($_SESSION['checked'])) { $_SESSION['checked'] = array(); } $_SESSION['checked'] = array_values(array_merge($_SESSION['checked'], $_GET['new_checked'])); ?> Quote Link to comment https://forums.phpfreaks.com/topic/192592-how-to-store-checkbox-array-in-a-session-and-then-be-able-to-add-to-it/#findComment-1014666 Share on other sites More sharing options...
Scooby08 Posted February 19, 2010 Author Share Posted February 19, 2010 Nice! I think that's working perfectly! I added one thing to get rid of duplicates.. $_SESSION['checked'] = array_unique(array_values(array_merge($_SESSION['checked'], $_GET['new_checked']))); That seems to work.. See any flaws with that? Quote Link to comment https://forums.phpfreaks.com/topic/192592-how-to-store-checkbox-array-in-a-session-and-then-be-able-to-add-to-it/#findComment-1014669 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.