简体   繁体   中英

How to delete a specific array item from PHP session

Im trying to delete an item from a session shopping cart. I used unset(), but somehow it didn't work

Link

<td width="100"><a href="?id=<?php echo $ids;?>&action=delete">
  <?php echo $ids;?></a></td>

Unset

if(isset($_GET['action'])&&($_GET['action']=="delete"))
{
    $new_id=$_GET['id'];
    unset($_SESSION['items'][$new_id]);
}

始终确保正确设置了get作为get参数传递的id ,并使用var_dump($_SESSION['items'])分析会话变量的结构,还应确保其匹配并注释代码。

unset session ok look this code result array(1) { ["id"]=> int(10) }

 <?php
  $_SESSION['items']=
  array(
  "id"=>10,
  "new_id"=>6
  );
    unset($_SESSION['items']["new_id"]);
    var_dump($_SESSION['items']);
?>

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