SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast DizzyDevil's Avatar
    Join Date
    Dec 2004
    Location
    Boston
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unset() from two dimensional array

    Is this possible the way I'm doing it? If so, does anyone know the proper syntax?

    PHP Code:

    if (isset($_GET['remove'])) {  
         
    $remove $_GET['remove'];   //value to be removed
        
    }
      
    if (
    in_array($remove$_SESSION ['cart'])) {  //check to see if value is in array
        
    unset($_SESSION['cart'][$remove]); // Remove item from $_SESSION['cart'] array
      


  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks fine to me. Why? Is there a problem?

  3. #3
    SitePoint Enthusiast DizzyDevil's Avatar
    Join Date
    Dec 2004
    Location
    Boston
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unset isn't working. $_SESSION['cart'] still holds the same value after.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure $_GET['remove'] holds a cart key and not a value? Can you show more code?

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I see it. I think you need array_key_exists() instead of in_array().

  6. #6
    SitePoint Enthusiast DizzyDevil's Avatar
    Join Date
    Dec 2004
    Location
    Boston
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I guess that's part of my problem. $remove is a value, not a key. So I guess my next question is obvious. How do I obtain the key?

  7. #7
    SitePoint Enthusiast DizzyDevil's Avatar
    Join Date
    Dec 2004
    Location
    Boston
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    array_search maybe? Would a search for the value return the key?

  8. #8
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple way would be to pass the key. If not, you can do:
    PHP Code:
    if (isset($_GET['remove'])) {  
         
    $remove $_GET['remove'];   //value to be removed
        
    }
      
    foreach(
    $_SESSION ['cart'] as $k=>$v) {  //check to see if value is in array
        
    if(isset($remove) && $v == $remove) {
            unset(
    $_SESSION['cart'][$k]); // Remove item from $_SESSION['cart'] array
        
    }


  9. #9
    SitePoint Enthusiast DizzyDevil's Avatar
    Join Date
    Dec 2004
    Location
    Boston
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks HardCoded, that worked nicely. I think I'll work on your suggestion of passing the key though


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •