justineaguas Posted July 23, 2010 Share Posted July 23, 2010 For example, I have a session array where I put values like this: $_SESSION['cart'][] = array("qty" => $qty, "p_id" => $p_id); How will I delete one element from this array? Quote Link to comment https://forums.phpfreaks.com/topic/208619-quick-question-how-can-i-delete-in-an-array-like-this/ Share on other sites More sharing options...
Alex Posted July 23, 2010 Share Posted July 23, 2010 You'll need to know the index of the $_SESSION['cart'] array that you're inserting into. Assuming it's, for example, 1, you can go about it like this: unset($_SESSION['cart'][1]['qty']); unset Quote Link to comment https://forums.phpfreaks.com/topic/208619-quick-question-how-can-i-delete-in-an-array-like-this/#findComment-1089911 Share on other sites More sharing options...
justineaguas Posted July 23, 2010 Author Share Posted July 23, 2010 Will foreach($array as $key => $value) work? $_SESSION[cart][$key]['qty'] - Will this work? Will the whole element be deleted? Including the p_id and qty? Quote Link to comment https://forums.phpfreaks.com/topic/208619-quick-question-how-can-i-delete-in-an-array-like-this/#findComment-1089932 Share on other sites More sharing options...
Alex Posted July 23, 2010 Share Posted July 23, 2010 If you want to delete all the elements of $_SESSION['cart']: foreach($_SESSION['cart'] as $key => $val) { unset($_SESSION['cart'][$key]); } Quote Link to comment https://forums.phpfreaks.com/topic/208619-quick-question-how-can-i-delete-in-an-array-like-this/#findComment-1089939 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.