douxu4610
2015-06-03 12:12
浏览 64
已采纳

将数组放入会话symfony2中

I have the following code :

 public function addAction(Request $request){
    $aBasket[] = $request->request->all();
    $this->get('session')->set('aBasket', $aBasket);
    return $this->redirect($this->generateUrl('shop_desktop_homepage'));
    print_r($aBasket);
}

Works fine but is saved in session only the last array wich was save. How to put in session. For example, the array is saved like this, only the last :

array:1 [▼
    0 => array:3 [▶]
]

But I want to save :

array:1 [▼
  0 => array:3 [▶]
  1 => array:3 [▶]
  2 => array:3 [▶]
]

not only the last. Please help me!! Thx in advance.

图片转代码服务由CSDN问答提供 功能建议

我有以下代码:

  public function addAction( 请求$ request){
 $ aBasket [] = $ request-> request-> all(); 
 $ this-> get('session') - > set('aBasket',$ aBasket)  ; 
返回$ this->重定向($ this-> generateUrl('shop_desktop_homepage')); 
 print_r($ aBasket); 
} 
   
 
 <  p>工作正常,但只保存在会话中保存的最后一个数组。 如何投入会议。 例如,数组像这样保存,只保存最后一个: 
 
 
 数组:1 [▼
 0 =&gt; 数组:3 [▶] 
] 
   
 
 

但我要保存:

 数组:1 [  ▼
 0 =&gt; 数组:3 [▶] 
 1 =&gt; 数组:3 [▶] 
 2 =&gt; 数组:3 [▶] 
] 
   
 
 

不仅是最后一个。 请帮助我!! Thx提前。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlaiyin2356 2015-06-03 12:19
    已采纳

    I have not tested the code. But it will help.

    public function addAction(Request $request){
    
      $aBasket = $request->request->all();
      // Get Value from session
      $sessionVal = $this->get('session')->get('aBasket');
      // Append value to retrieved array.
      $sessionVal[] = $aBasket;
      // Set value back to session
      $this->get('session')->set('aBasket', $sessionVal);
      return $this->redirect($this->generateUrl('shop_desktop_homepage'));
    
    }
    

    I have wrote comment. I have not checked value return by session get method. you need to check type of value return from session get.

    Hope this help.

    点赞 评论

相关推荐 更多相似问题