Cakephp 3 How to make session array


I am trying to write session in controller. My structure is

$_SESSION['a'][0] = 1; $_SESSION['a'][1] = 2; $_SESSION['a'][2] = 3;

And I am trying this

Configure::write('Session', ['a' =>'1'])

But it is not working. How do this in cakephp 3 way


To write variable in Session in CakePHP 3 you need to write following code :

$this->request->session()->write('Your Key',Your_array);

To know more information you can visit here :

<a href="http://book.cakephp.org/3.0/en/development/sessions.html" rel="nofollow">http://book.cakephp.org/3.0/en/development/sessions.html</a>


You can simply use

$session->write([ 'key1' => 'blue', 'key2' => 'green', ]);

I am refering to

<a href="http://book.cakephp.org/3.0/en/development/sessions.html#reading-writing-session-data" rel="nofollow">http://book.cakephp.org/3.0/en/development/sessions.html#reading-writing-session-data</a>


<strong>The answer is that this cannot be done in CakePHP 3.x</strong>

In vanilla PHP, it's possible to do this:

<?php session_start(); $_SESSION['a'][0] = 1; $_SESSION['a'][1] = 2; $_SESSION['a'][2] = 3; var_dump($_SESSION); ?>

Which will output:

array(1) { ["a"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } }

This is correct, and what <em>should</em> happen.

In CakePHP, you cannot specify arrays in the session key. For example:

$this->request->session()->write('a[]', 1); $this->request->session()->write('a[]', 2); $this->request->session()->write('a[]', 3);

Will not work.

If you remove the [] <strong>the value will get overwritten</strong>. For example:

$this->request->session()->write('a', 1); $this->request->session()->write('a', 2); $this->request->session()->write('a', 3);

The value of $this->request->session()->read('a') would be 3. The values 1 and 2 have been overwritten. Again, this is to be expected because you're overwriting the key a each time. The equivalent vanilla PHP for this is:

$_SESSION['a'] = 1; $_SESSION['a'] = 2; $_SESSION['a'] = 3;

Due to the lack of an indexed array, $_SESSION['a'] gets overwritten each time. This is normal behaviour. It needs to have the indexes (e.g. ['a'][0], ['a'][1], ...) to work!

The other answers where they have given things like key1 and key2 are not appropriate. Because there are many situations where you want everything contained within an indexed array. Generating separate key names is wrong for this type of scenario.


  • nested std::map using pointers
  • get key value pairs from xml using linq
  • Foreign key pointing to Compound Key
  • Issue with session.handler.native_file session handler in symfony2
  • Why the query doesn't return results when the ancestor is not provided?
  • PHP Post & Redirect with cURL Same As HTML Form [closed]
  • IP and domain create different session
  • Which table should be Parent table and which should be child table?
  • jQuery and Uploadify session in the php file
  • Capturing STDOUT in RSpec
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Unable to connect to OnVif enabled camera using C#
  • .NET video play library which allows to change the playback rate?
  • Getting different value with placeholder over CPU/GPU
  • How can I include If-None-Match header in HttpRequestMessage
  • Can I programmatically choose the Android layout folder?
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Does Mobilefirst provide a provision to access web services directly?
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Parse a date string in a specific locale (not timezone!)
  • ADO and msqli connections very slow
  • ImageMagick, replace semi-transparent white with opaque white
  • Cannot connect to cassandra from Spark
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Cross-Platform Protobuf Serialization
  • Javascript Callbacks with Object constructor
  • How can I use Kendo UI with Razor?
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • embed rChart in Markdown
  • JaxB to read class hierarchy
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app