title: Adding to an array in $_SESSION
body: <p>I have a survey script that has 3 questions per page. When users answer the questions on the first page and click next, the data from the previous page is stored in <code>$_SESSION['survey']['data']</code> by doing this:</p>
  
  <pre><code>$data = postToArray($_POST, $ignore_fields);
  $_SESSION['survey']['data'] = $data;
  </code></pre>
  
  <p><code>$data</code> is an array that looks like:</p>
  
  <pre><code>array('question' =&gt; 'answer', 'question' =&gt; 'answer');
  </code></pre>
  
  <p><code>postToArray</code> does a few checks and manipulates the actual submission a bit, before returning it to <code>$data</code>.</p>
  
  <p>When the user is on page two of the survey, the same thing happens over. I assumed that when <code>$data</code> gets added to the session, via <code>$_SESSION['survey']['data'] = $data;</code>, that it would append to the session array if the <code>'question'</code> (key) did not exist, but if it did (because the user went to a previous page and changed their answer), that the existing value with the same key would be overwritten, however the last page's submission overwrites everything in the <code>['data']</code> array in the session. Come to think about it, that makes perfectly sense.</p>
  
  <p>I tried various things, such as retrieving the <code>$_SESSION['survey']['data']</code>, storing it in array, reading the last submission, merging the arrays, and then re-saving everything in the SESSION, but my code didn't work out -- does this approach make sense? Is that possible?</p>
  
  <p>I also tried <code>array_push</code>, but no luck there.</p>
  
  <p>In addition, I tried adding to <code>$_SESSION['survey']['data'][]</code>, which at least saves everything (each submission in its own array), but then if the user goes back a page, any values they change and re-submit are added as another array.</p>
  
  <p>Preferably, I'd like one giant array with all questions/answers and it keeps adding to that array and overwrites any values with existing keys.</p>
  
  <p>What's the best approach?</p>
  
  <p>Thanks,
  -Ryan</p>
  
  <p><strong>SOLUTION IMPLEMENTED</strong></p>
  
  <pre><code>$data = postToArray($_POST, $ignore_fields);
  foreach($data as $question =&gt; $answer)
  {
      $_SESSION['survey']['data'][$question] = $answer;
  }
  </code></pre>