int(0) ["currentItem"]=> string(1...">

Removing an array from a multidimensional array

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 certain conditions. I tried the following.

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

It doesn't seem to work, it doesn’t delete anything, I did var_dump to check if the variables $ stoeltje and $ id were ok, and they were ok, so this may not be a problem. Can I use unset in this situation?

+4
source share
4 answers
 foreach ($_SESSION["addedToCart"] as &$arr) 

& turns your variable into a link instead of a copy. Normally, that would be enough. unset() only works with data in the current area (so your foreach loop) leaves the original unchanged (see unset () for details).

Instead, you can:

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

This does not work because foreach is working on a copy, so $ arr is just a copy of each element in the main table.

from php.net:

Starting with PHP 5, you can easily change the elements of an array by specifying the previous $ value with &. This will cause the link to be assigned 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) 
+1
source

Try the following:

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

Even if the proposed method with the link should work fine, here is an example without it:

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

All Articles