We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Array in sessions

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



25.0k

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"]



17.5k

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


17.5k

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);