简体   繁体   中英

Items seem not to be added to the session array

So I have some php trying to add an item onto the end of an array stored in $_SESSION['basket'] using the code array_push($_SESSION['basket'],$_POST['item']); . The first item will be added just fine, however once there is an item present nothing else seems to be added to the array. Thanks in advance.

You need to reassign your $_SESSION variable. Simply calling array_push() on a session variable will not change it.

Something to the effect of:

$basket = array_push($_SESSION['basket'], $_POST['item']);
$_SESSION['basket'] = $basket;

should do the trick.

This, of course, assumes that $_SESSION['basket'] is nonempty and you have access to your session variables.

Try this:

$_SESSION['basket'][] = $_POST['item'];

If your array uses numeric index, is more efficiently than use of array_push.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM