将数组放入会话 symfony2 [英] Put arrays in session symfony2

查看:52
本文介绍了将数组放入会话 symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 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 which was save. How to put in session. For example, the array is saved like this, only the last:

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

但我想保存:

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

不仅仅是最后一个.

推荐答案

我没有测试代码.但它会有所帮助.

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'));

}

我已经写了评论.我没有检查 session get 方法的值返回.您需要检查会话获取返回的值类型.

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.

希望对您有所帮助.

这篇关于将数组放入会话 symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆