dongpao1921 2013-06-27 11:41
浏览 31
已采纳

PHP会话在多维时不会持久化

I'm having a little issue, when I do this:

$_SESSION['cartItems'] = array();

It works fine, and it creates a cartItems array in the session.

But when I do this:

$_SESSION[2]['cartItems'] = array();

This works until I refresh the page, then it forgets this session array?

I tried to create the 2 array first:

$_SESSION[2] = array();
$_SESSION[2]['cartItems'] = array();

But still does not work like it is supposed to do.

How can I solve this?

  • 写回答

2条回答 默认 最新

  • dsdt66064367 2013-06-27 11:45
    关注

    Numeric keys are converted to strings because the $_SESSION variable is an associative array. You might want to try using $_SESSION["2"] when assigning or retrieving values.

    Further more, it looks to me like you are trying to persist several cart arrays, so why not use something like this instead:

    $_SESSION['carts'] = array();
    $_SESSION['carts'][0] = array();
    $_SESSION['carts'][1] = array();
    ...
    

    Or even $_SESSION['carts'][0]['cartItems'] = array()

    Some related posts for further reading:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?