title: CakePHP 3 - How to add to an already set view var of type array
body: <p>I'm building a RESTful API which is returning a JSON response. When setting the view vars in the controller, is there a way to explicitly set elements of an array variable which already exists?</p>
  
  <p>For example, let's say the <code>AppController</code> sets a view var which is an array:</p>
  
  <pre><code>$this-&gt;set('myArray', array('foo' =&gt; 'bar'));
  </code></pre>
  
  <p>But then in the child controller <code>MyController</code> I want to add to that array. Something like:</p>
  
  <pre><code>$this-&gt;set('myArray.add', 'New Element');
  </code></pre>
  
  <p>So that the JSON response for the view will be:</p>
  
  <pre><code>{
      "myArray": {
          "foo": "bar",
          "add": "New Element"
      }
  }
  </code></pre>
  
  <p>I'm guessing it can be done in a roundabout way by storing the array as a normal PHP property of the <code>AppController</code> class, and then adding to the array in the child controller before finally doing <code>$this-&gt;set()</code> once at the end of the controller action.</p>
  
  <p>But I'm curious if there is a way to do this through the <code>Controller::set()</code> method alone.</p>