unset() unsets more than it should from $_SESSION subarray

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gm04030276
    New Member
    • Nov 2006
    • 17

    unset() unsets more than it should from $_SESSION subarray

    hey, i'm basically trying to use php sessions and a bit of javascript to make tabbed browsing within a web page using sessions to store the data of the different opened pages.
    Problem: when i unset the first 0 index in the array (ie close the default page) it seems to wipe the other one (can't get it to add more that one anymore) from the array also

    the information is held in sub array
    [PHP]$_SESSION['pages'][/PHP]
    each page has its own array of infomation
    [PHP]$_SESSION['pages'][<pageindex>]['title' => value, 'link' => value][/PHP]

    the menu down the side uses a foreach loop to echo out each of the pages and links in the ['pages'] array using
    [PHP]foreach($_SESSI ON['pages'] as $tid => $page){...}[/PHP]
    so that i can get the tab id for the page to unset it from the array
    there is a link echoed for each page like so:
    [HTML]<a href="menu.php? action=deleteta b&tid='.$tid. '" target="menu">[/HTML]

    so that when its clicked, we go to the menu page where we have the following code at the top (the menu page is self contained to do all of this)

    [PHP]
    switch($_REQUES T['tid']){
    case "deletetab" :
    unset($_SESSION['pages'][$_REQUEST['tid']]);
    break;
    <other code for other actions>
    Default:
    break;
    }
    [/PHP]

    Note: it did use to work correctly when i use a for loop to itterate through the different tabs but then this would have failed because it would after one deletion, have the wrong array id (tid) number for the pages after the one we deleted and also others wouldn't have been displayed that had a higher id number because the for loop used a count($_SESSION['pages']) function to determine how many there were and how high $i should count in the loop, but if we delete from the middle, there are less even though the id's haven't changed....they wern't all displayed! but this new way seems to delete all the data when the default one is clicked, though if you click to delete the second array object, it still leaves the first intact.

    Thanks in advance!
  • gm04030276
    New Member
    • Nov 2006
    • 17

    #2
    Anyone? please?! .

    Comment

    • niccolo
      New Member
      • Feb 2012
      • 1

      #3
      This answer is about 5 years late. However I came across the same problem and found no help online. So here is the solution in case anyone else ends up here.
      Your problem is that when you call unset($_SESSION['pages'][$_REQUEST['tid']]);
      PHP will unset everything in $_SESSION['pages'], not just the sub-array.
      You need to address it like this:
      foreach ($_SESSION['pages'] as $key => $value) {
      if ($key == $_REQUEST['tid']) unset($_SESSION['pages'][$key]); }
      }
      You are unsetting only the $key from within the parent array.
      Last edited by niccolo; Feb 27 '12, 12:06 AM. Reason: edited as I used unset($key) instead of unset($_SESSION['pages'][$key]);

      Comment

      Working...