开发者

How To Get Array From Session Object In Kohana?

开发者 https://www.devze.com 2023-03-15 13:38 出处:网络
I\'m using Kohana 3.0 and I need to get array from Session object. For example: $session = Session::instance();

I'm using Kohana 3.0 and I need to get array from Session object.

For example:

$session = Session::instance();

$session->set(
    'myArray'
    array(
        'key1' => 'foo',
        'key2' => 'bar'
    )
);

// How to get specific array element?

I thought that this will work, but it returned null.

$session->get('myArray.key2');

After thinking and thinking (ha, ha!) I think-out this...

$myArray = $session->get('myArray');
$key1 = $myArray['key1'];

Is it okay? Is there better w开发者_如何学运维ay?

P.S. When array dereferencing will be available... gonna use it! =]

$key1 = $session->get('myArray')['key1']; // Lets hope that this work!


  1. Retrieve basic var and use standard Arr helper

     Arr::get($session->get('myArray', array()), 'key1');
    
  2. Work with session data as array:

     $data = & $session->as_array();
     $key1 = Arr::path($data, 'myArray.key1');
     $data['myArray']['key2'] = 'foo'; // also you can set data
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号