Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing array from multidimensional array

Tags:

php

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?

like image 685
vincent Avatar asked Aug 13 '10 06:08

vincent


People also ask

How do I remove something from a 2D array?

To delete an element in a 1D array, switch to the front panel, right-click the array element, and select Data Operations»Delete Element from the shortcut menu. To delete a row or column in a 2D array, right-click the array row or column and select Data Operations»Delete Row or Delete Column.

Is a multidimensional array an array of arrays?

A multidimensional array is an array containing one or more arrays.

How do you clear a multidimensional array in PHP?

Using "array(array())" will create a 2D array with an "empty" element in the first position. To create a truly blank 2D array this needs to be removed. <? php $emptyArray = array(array()); // Creates a 2D array with one empty element in $emptyArray[0] array_pop($emptyArray); // Pops element[0] off the array ?>

How do you remove an array from a key?

Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array.


2 Answers

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]);
   }
}
like image 88
jasonbar Avatar answered Sep 30 '22 14:09

jasonbar


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]);
       }
 }
like image 37
Alex Avatar answered Sep 30 '22 14:09

Alex