ling
ling

Reputation: 155

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]);
}

Upvotes: 1

Views: 60

Answers (2)

william.eyidi
william.eyidi

Reputation: 2365

Always make sure the id you are passing as a get parameter is set properly, and analyse the structure of your session variable with a var_dump($_SESSION['items']), you should make sure it matches and comment your code as well.

Upvotes: 1

Tuan Huynh
Tuan Huynh

Reputation: 11

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']);
?>

Upvotes: 1

Related Questions