Array in sessions

How change array in session? example $_SESSION['var']['var2'] = 1;



10.8k

Zach, u can show example?

edited Jul '15

Phalcon's built-in session handler only allows you to do key/value setting - not deep setting inside an array.

@stanleer - did you try what you exampled? That should work, provided you've actually set $_SESSION["var"] and $_SESSION["var"]["var2"]



10.2k

I store nested, mluti-dimensional arrays in session:

Inside of a model:

    public function doSomething() {
        $array = [
            "value1" => "Awesome",
            "value2" => "Heay, neat",
            "nested1" => [
                "nestedvalue1" => "Super!",
                "nestedvalue2" => "Excellent"
            ]
        ];
        $this->getDi()->getShared('session')->set('sessionval', $array);
        return true;
    }

Inside of a controller, you can set by:

    $this->session->set('sessionval',$array);

Retrieve by calling:

    $this->session->get('sessionval'); //in a controller
    $this->getDi()->getShared('session')->get('sessionval'); //in a model


10.2k

To further answer your question...

Get the session array:

    $sessionVal = $this->session->get('sessionval');

Then do what you need to do with the array:

    $sessionVal['nested1']['nestedvalue1'] = "Super duper!";

Then store the value:

    $this->session->set('sessionval',$sessionVal);