Zend框架2 - 包含多维数组会话 [英] Zend Framework 2 - Sessions containing multidimensional arrays
问题描述
假设我有以下一组数据:
Suppose i have the following set of data:
$foobar = array(
"foo" => array (
"foo1" => 1,
"foo2" => 2,
"foo3" => 3
),
"bar" => array (
"bar1" => 1,
"bar2" => 2,
"bar3" => 3,
),
);
在标准的PHP,我可以做到以下几点:
In standard PHP, i could do the following:
$_SESSION['foobar'] = $foobar;
然后,打电话值,通过实例BAR2:
Then, to call values, by example bar2:
$_SESSION['foobar']['bar']['bar2'];
但是,我们在Zend框架2这样做呢?
But what about doing this in Zend Framework 2?
我已经设置引导与会话管理器的所有参数,集装箱已设置它。会议得到建立。所以,如果我这样做,举例:
I have already set bootstrap with all parameters for session manager, and container has been set with it. Sessions get created. So, if i do, by example:
$session = new Container('foobar');
和在那里放一个值:
$session->foo1 = 1;
这工作。同样的,如果我决定把数组作为会话变量:
this works. Same if i decide to put an array as session variable:
//placing the $foobar array defined before
$session->foobar = $foobar;
但我不知道我怎么能叫值。假如我想foo2的,我会做
But i don't know how can i call values. Supposing i want foo2, i'd do
echo $session->foobar->foo->foo2;
期待它会输出'2',但我得到而不是错误:
于是,我就做
echo $session->foobar['foo']['foo2'];
但返回另一个错误。
but this returns another error.
所以,现在我不知道我应该怎么做来收集这些数据,或者我怎么可以存储会话变量不同。我需要这个做一个购物车,那么foo和bar是不同的产品。我怎么能这样做呢?
So now i don't know what should i do to gather those data, or how could i store session variables differently. I need this to make a shopping cart, so foo and bar are different products. How could i do this?
推荐答案
解决。首先,我创建父抵消这种方式:
Solved. First of all i created the parent offset this way:
$session->offsetSet("foobar", new ArrayObject());
(需要使用Zend的\\ STDLIB \\ ArrayObject的;
在你的脚本的顶部)。
现在我可以从那里创造什么:
Now i can create anything from there:
$session->foobar->foo = "foo1";
$session->foobar->bar = "bar1";
等回事。
为了得到他们,这是那么容易,因为它应该:
and so going on. To get them, it's as easy as it should:
echo $session->foobar->foo; //returns foo1
我希望这会帮助别人。
I hope this will help someone.
这篇关于Zend框架2 - 包含多维数组会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!