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

Storing an Array in a Phalcon Session

HI All,

How can I store an array value in a phalcon session and also update the array value when required? I am rewriting a legacy system and it uses a lot of $_SESSION vars all over the place.

This is an example array that is saved in the session :

$summary = array('name' => "Joe", age => 50);
//Addresses
$address1 = array('street_no' => 50, 'name' => "Atlantis");
$address2 = array('street_no' => 50, 'name' => "Atlantis Town");
// Normal session
$_SESSION['person']['summary'] = $summary;
$_SESSION['person']['summary']['address'] = $address;
// updating the normal sesison address
unset($_SESSION['person']['summary']['address']);
$_SESSION['person']['summary']['address'] = $addres2;

How would I then set and update the session details in Phalcon? I need to use the exact same structure.

I already use the following but how can I update the exsiting session data.

// Using the phalcon method.
$session->set('person', $person);
//now only update the address of the session
?????

Thanks

edited Feb '15

You can use the session like:

$session->person = ['name' => 'john', 'age' => 25];
//or
$session->set('person', ['name' => 'john', 'age' => 25]);

$session->person['age']++;

echo $session->person['age'];
//26

$session->person['age'] = 27;

print_r($session->person);
//Array ( [name] => john [age] => 27 )