Reputation: 205
I have some code here which looks as following:
if (isset($_SESSION['addToCart'])) {
foreach ($_SESSION["addToCart"] as &$value){
if ($value['titel'] == $titel){
$value['aantal'] = $aantal;
if($value['aantal'] == 0){
unset($value);
}
}
}
}
so when 'aantal' = 0, I want to delete that record, but it doesn't, it just gives back the result and 'aantal' is 0 instead of the record being removed from the session.
Anyone know what I'm doing wrong?
Upvotes: 0
Views: 121
Reputation: 67745
From the manual: "When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed."
You'd want to do something like this:
foreach ($_SESSION['addToCart'] as $key => &$value) {
if ($value['aantal'] == 0) {
unset($_SESSION['addToCart'][$key]);
}
}
Think of it like filesystem's symbolic links. When you destroy a symbolic link, it doesn't destroy the file it was linked to.
Upvotes: 0
Reputation: 1553
According to the PHP docs (http://php.net/manual/en/function.unset.php), a variable that is passed by reference is only destroyed in the local context. Try $value = null;
Upvotes: 1
Reputation: 2594
Your problem may stem from the fact that $value is a value variable, a copy of the $_SESSION["addToCarT"][current_index]. You should be setting the $_SESSION["addToCart"][current_index] to null or unsetting that, not the copies variable in the limited scope.
Upvotes: 0