almystersv Posted January 12, 2008 Share Posted January 12, 2008 Hi Guys I have this page that I am trying to use to remove one item at a time from the array. At present it just competely empties the array. Please help... <?php session_start(); foreach($_SESSION['basket'] as $key => $product) { if ($_SESSION['basket'][$key]['URN'] == $product['URN']) { unset($_SESSION['basket'][$key]); } } header("Location: StationaryMain.php"); exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85669-removing-items-individually-from-an-array/ Share on other sites More sharing options...
almystersv Posted January 13, 2008 Author Share Posted January 13, 2008 Please help Quote Link to comment https://forums.phpfreaks.com/topic/85669-removing-items-individually-from-an-array/#findComment-437594 Share on other sites More sharing options...
Ken2k7 Posted January 13, 2008 Share Posted January 13, 2008 Well is the line below always true? if ($_SESSION['basket'][$key]['URN'] == $product['URN']) Quote Link to comment https://forums.phpfreaks.com/topic/85669-removing-items-individually-from-an-array/#findComment-437605 Share on other sites More sharing options...
p2grace Posted January 13, 2008 Share Posted January 13, 2008 Do you want to unset the entire row? Or just the "urn" key? If you just want to unset the urn key try this: unset($_SESSION['basket'][$key]['URN']); Quote Link to comment https://forums.phpfreaks.com/topic/85669-removing-items-individually-from-an-array/#findComment-437607 Share on other sites More sharing options...
almystersv Posted January 13, 2008 Author Share Posted January 13, 2008 no i would like to delete the entire row that matches that URN. Im using it as a shopping cart example and need a remove function. Quote Link to comment https://forums.phpfreaks.com/topic/85669-removing-items-individually-from-an-array/#findComment-437899 Share on other sites More sharing options...
tinker Posted January 13, 2008 Share Posted January 13, 2008 What youve got: foreach($_SESSION['basket'] as $key => $product) { if ($_SESSION['basket'][$key]['URN'] == $product['URN']) { unset($_SESSION['basket'][$key]); } } What this tells me is that if x[y] == x[y] (albeit different names) then unset, and obviously it will unset everything because it's testing against itself! Is that clear? You need to test it against something other than itself. Quote Link to comment https://forums.phpfreaks.com/topic/85669-removing-items-individually-from-an-array/#findComment-437901 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.