2010-08-29 126 views
1

的一部分,我有一个会议看起来像这样:覆盖会话

 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;

+1

你能告诉我们的代码,你打算值赋给$ _SESSION阵列的其他指标?另外,var_dump中没有'addToCart'索引,它来自哪里? – erisco 2010-08-29 15:28:31

+1

你可以编辑你的文章,并添加代码如何分配新值。 – Youssef 2010-08-29 15:31:24

+0

@erisco,只是因为我vardumped的内容addToCart – vincent 2010-08-29 15:39:40

回答

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

应该这样做。

+0

不,不工作 – vincent 2010-08-29 15:34:07

+2

确实工作,我的错误 – vincent 2010-08-29 15:51:12

2

比方说,你希望把25“stoeltjes”然后,像这样做:

$_SESSION['addToCart'][$id]["stoeltjes"] = 25; 
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";