SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Jan 16, 2005, 09:38 #1
- 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
}
-
Jan 16, 2005, 09:43 #2
- 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?
-
Jan 16, 2005, 09:50 #3
- 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.
-
Jan 16, 2005, 09:52 #4
- 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?
-
Jan 16, 2005, 09:54 #5
- 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().
-
Jan 16, 2005, 09:57 #6
- 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?
-
Jan 16, 2005, 10:03 #7
- 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?
-
Jan 16, 2005, 10:04 #8
- 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
}
}
-
Jan 16, 2005, 10:19 #9
- 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