PHP:通过可变变量设置会话变量
我想设置一个类似于以下内容的会话变量:
$key = '_SESSION[element]';
$$key = 'value';
这确实将 $_SESSION['element']
设置为 value
,但它似乎也清除了其余部分我的 $_SESSION
变量,导致 $_SESSION
数组仅包含新的键/值对。
如何使用可变变量写入会话而不破坏它?
编辑:如果这无法完成,那就这样吧,我们可能必须重组并以“正确”的方式做事。我只是想知道是否有一个简单的解决办法
I would like to set a session variable with something akin to:
$key = '_SESSION[element]';
$key = 'value';
This does indeed set $_SESSION['element']
equal to value
, but it also seems to clear the rest of my $_SESSION
variable, resulting in the $_SESSION
array only containing the new key/value pair.
How can I write into the session using variable variables without nuking it?
Edit: if this can't be done, so be it, we'll probably have to restructure and do things the "right" way. I just wanted to know if there was an easy fix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Mala,我认为 eval 会帮助你。
检查下面的代码。它可能会帮助你得到你想要的。
如果您还有任何问题,请告诉我。谢谢
@Mala, I think eval will help you.
Check the code below. It may help you for what you want.
If you still have any trouble, Let me know. Thank you
来自 PHP 文档:
你是如何最终陷入这样的境地的,这确实值得怀疑。你可能做错了什么。
编辑
这个小技巧应该给你你想要的:
这基本上会产生与xdazz的答案相同的结果
From PHP Documentation:
How you ended up with a situation like this, is really questionable. You're probably doing something wrong.
EDIT
This little trick should give you what you want:
This will basically produce the same results as xdazz's answer
这样不是更好吗?
Isn't this way better?