6

I have a session that looks like this:

array(3) {
  ["counter"]=>
  int(0)
  ["currentItem"]=>
  string(1) "2"
  ["addedToCart"]=>
  array(12) {
    [0]=>
    array(11) {
      ["aantal"]=>
      int(1)
      ["id"]=>
      string(1) "1"
      ["filmtitel"]=>
      string(11) "a_bugs_life"
      ["film_id"]=>
      string(1) "2"
      ["zaal_id"]=>
      string(1) "1"
      ["zaaltitel"]=>
      string(6) "zaal 1"
      ["tijdstip"]=>
      string(8) "15:00:00"
      ["stoeltjes"]=>
      string(2) "21"
      ["dag"]=>
      string(8) "woensdag"
      ["verwijder"]=>
      int(2)
      ["vertoningId"]=>
      string(1) "3"
    }
    [1]=>
    array(11) {
      ["aantal"]=>
      int(1)
      ["id"]=>
      string(1) "1"
      ["filmtitel"]=>
      string(11) "a_bugs_life"
      ["film_id"]=>
      string(1) "2"
      ["zaal_id"]=>
      string(1) "1"
      ["zaaltitel"]=>
      string(6) "zaal 1"
      ["tijdstip"]=>
      string(8) "15:00:00"
      ["stoeltjes"]=>
      string(1) "7"
      ["dag"]=>
      string(8) "woensdag"
      ["verwijder"]=>
      int(2)
      ["vertoningId"]=>
      string(1) "3"
    }
    [2]=>
    array(11) {
      ["aantal"]=>
      int(1)
      ["id"]=>
      string(1) "1"
      ["filmtitel"]=>
      string(11) "a_bugs_life"
      ["film_id"]=>
      string(1) "2"
      ["zaal_id"]=>
      string(1) "1"
      ["zaaltitel"]=>
      string(6) "zaal 1"
      ["tijdstip"]=>
      string(8) "15:00:00"
      ["stoeltjes"]=>
      string(2) "22"
      ["dag"]=>
      string(8) "woensdag"
      ["verwijder"]=>
      int(2)
      ["vertoningId"]=>
      string(1) "3"
    }
  }
}

Now, from $_SESSION['addedToCart] I would like to remove arrays if they meet to certain conditions. I have tried the following.

foreach ($_SESSION["addedToCart"] as $arr) {
       if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) {
            unset($arr);
       }
 }

This doesn't seem to work, it doesn't remove anything, I did a var_dump to check if the variables $stoeltje and $id were fine and they were fine so that cant be the problem. Am I able to use unset in this kind of situation?

5 Answers 5

8
foreach ($_SESSION["addedToCart"] as &$arr)

& turns your variable into a reference instead of a copy. Normally this would be sufficient. unset() only works on data within the current scope (so your foreach loop) leaving the original unchanged (See unset() for details).

Instead you can do:

foreach ($_SESSION["addedToCart"] as $key => $val)
{
    if ($val["stoeltjes"] == $stoeltje && $val['film_id'] == $id) {
        unset($_SESSION["addedToCart"][$key]);
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

i modified it like you say: foreach ($_SESSION["addedToCart"] as &$arr) { if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) { echo"test"; unset($arr); } } but it still doesnt work, id does print the echo thou so i'm definitly getting into the if-statement
2

Even if the suggested way with the reference should work normally, here's an example without it:

foreach ($_SESSION["addedToCart"] as $key => $arr) {
       if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) {
            unset($_SESSION["addedToCart"][$key]);
       }
 }

Comments

1

It doesn't work, because foreach is working on a copy, therefore $arr is just a copy of each element in the main table.

from php.net:

As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)

2 Comments

i modified it like you say: foreach ($_SESSION["addedToCart"] as &$arr) { if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) { echo"test"; unset($arr); } } but it still doesnt work, id does print the echo thou so i'm definitly getting into the if-statement
Yes. unsetting $arr just deleted the reference and the main table was still referencing same thing. I'd fix that, but You already got a full answer up there ;)
1

Try this:

$arr = array(1, 2, 3, 4); 
foreach ($arr as $key => &$value) { 
    if ($value == 2)
    {
        unset($arr[$key]);
    }
} 
print_r($arr);

Comments

0

remove_array_key("film_id", $array);

function remove_array_key($key, &$array)
{
    $result = array_key_exists($key, $array);

    if ($result) {
        unset($array[$key]);
        return $array;
    }

    foreach ($array as &$v) {
        if (is_array($v)) {
            $result = remove_array_key($key, $v);
        }
        if (is_array($result)) {
            unset($v[$key]);
            return $array;
        }
    }

    return false;
}

Link to Github Explanation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.