覆盖会话的一部分

时间:2010-08-29 15:24:15

标签: php

我的会议看起来像这样:

        array(1) {
  [31]=>
  array(10) {
    ["aantal"]=>
    int(1)
    ["id"]=>
    string(2) "31"
    ["filmtitel"]=>
    string(16) "2_fast_2_furious"
    ["film_id"]=>
    string(1) "1"
    ["zaal_id"]=>
    string(1) "1"
    ["dag"]=>
    string(8) "woensdag"
    ["zaaltitel"]=>
    string(6) "zaal 1"
    ["tijdstip"]=>
    string(8) "17:30:00"
    ["stoeltjes"]=>
    array(3) {
      [0]=>
      string(2) "20"
      [1]=>
      string(2) "21"
      [2]=>
      string(2) "22"
    }
    ["aantalStoeltjes"]=>
    string(3) "150"
  }
}

我的问题是,如何覆盖["stoeltjes"]的内容?

当我做unset($_SESSION['addToCart'][$id]["stoeltjes"]);

然后["stoeltjes"]被删除但是当我添加其他值时,它们会被放入stoeltjes数组中的额外数组中。

我在下面分配了新值:$_SESSION["addToCart"][$id]["stoeltjes"][] = $seats;

3 个答案:

答案 0 :(得分:3)

$_SESSION['addToCart'][$id]["stoeltjes"] = "new value";

应该这样做。

答案 1 :(得分:2)

假设你想在[“stoeltjes”]中加入25,那么就这样做:

 $_SESSION['addToCart'][$id]["stoeltjes"] = 25;

答案 2 :(得分:1)

额外[]告诉代码您希望变量中有一个新的数组元素。

这样做:

$_SESSION["addToCart"][$id]["stoeltjes"][] = "new value";
$_SESSION["addToCart"][$id]["stoeltjes"][] = "new value";
$_SESSION["addToCart"][$id]["stoeltjes"][] = "new value";

相当于这样做:

$_SESSION["addToCart"][$id]["stoeltjes"][0] = "new value";
$_SESSION["addToCart"][$id]["stoeltjes"][1] = "new value";
$_SESSION["addToCart"][$id]["stoeltjes"][2] = "new value";