更新会话数组+ zf2的节点

时间:2013-01-25 11:07:57

标签: php session zend-framework2

首先我添加了一个会话然后我打印我的会话它看起来完全没问题

Array
(
[14] => Array//(main key is my restaurant Id)
    (
        [retaurantDetail] => Array
            (
                [restId] => 14
                [restaurantName] => Barca
                [published] => 1
                [timings] => 10 to 10
                [normalCost] => 150
                [logo] => 44f7afcffb0aeea5c69ccee9041cab84.jpg
                [email] => barca@barca.com
                [phone] => 741258
            )

        [menuArray] => Array
            (
                [70] => Array // (menu ID is the Key)
                    (
                        [menuId] => 70
                        [productId] => 35
                        [productName] => Coca Cola
                        [categoryTitle] => Beverages
                        [categoryId] => 52
                        [price] => 100
                        [attributeName] => 1.5L
                        [isDefault] => 1
                        [qty] => 1
                    )

            )

    )
)

现在我添加了一些逻辑,如果有人再次添加菜单添加到购物车中他/她在这个菜单中添加加1个数量这是我的代码,(代码尚未完成但现在我专注于更新数量)

            foreach($session->cartSession as $sessionKey=>$sessionVal)
            {

                        foreach($sessionVal['menuArray'] as $sessionMenuKey=>$sessionMenuVal)
                        {

                            if($sessionMenuKey == $post_data['menuId'])
                            {
                                echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>";
                                //$qty = $session->cartSession[$restaurantDetail['restId']]['menuArray'][$sessionMenuKey];
                                $sessionMenuVal['qty'] =  $sessionMenuVal['qty']+1;

                                echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>"; 

                            }

                         }
            }

我想要的是我想更新会话数量。

每次我按添加它显示1数量然后我加上1加值然后它显示2然后它再次显示1数量:(。

1 个答案:

答案 0 :(得分:2)

你永远不会写回会议......

如果我正确理解您的代码,以下内容应该有效。检查我的代码的正确数组嵌套,以防它没有开箱即用。

// Somewhere on top, use this for your loops
$cartSession = $session->offsetGet('cartSession'); 

// Inside your matched loop
$currentQty = $sessionMenuVal['qty'];
$cartSession[$sessionKey][$sessionMenuKey]['qty'] = ++$currentQty;

// After your loops at the end
$session->offsetSet('cartSession', $cartSession);