title: Write a function that puts a list of items from a form into an array (text field) using PhP/HTML
body: <p>The problem: Write a function that puts a list of items from a form into an array (text field) using PhP/HTML.</p>
  
  <p>Tools to use: PhP and HTML.</p>
  
  <p>So I have been looking around a lot to find a solution to this problem but so far have been unsuccessful. Essentially I want a text field for user input and a submit button. Once the text has been submitted it is added to an array which consists of all previously entered text. After digging around it seems that 2 possible solutions to this problem would be to create PhP Sessions or the use of Hidden fields. I have minimal to no knowledge of how to implement Sessions or hidden fields.</p>
  
  <p>Here is the code I wrote so far(trying many different variations), but I still end up with the same results, the newest text overwrites the existing array so the output is always "Array[0] => string". Any help in learning this would be awesome.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  
  $item = [];
  
  function itemList() {
    if(isset($_POST['string'])) {
      $pieces = explode(" ", $_POST['string']);
      $item[$pieces] = $_POST['item'];
      return $item[$pieces];
    }
    if(isset($_POST['item'])) {
      foreach($_POST['item'] as $key =&gt; $pieces) {
        foreach($pieces as $key =&gt; $piece) {
          $item[$pieces] = $piece;
        }
      }
    }
    return "&lt;input type='hidden' name='item' value='&lt;?php $_POST['item']; ?&gt;' /&gt;";
  }
  
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="POST"&gt;
      &lt;input type="text" name="string" /&gt;
      &lt;input type="submit" value="Submit" /&gt;
      &lt;input type="hidden" name="item" value="&lt;?php $_POST['item']; ?&gt;" /&gt;
  &lt;/form&gt;
  &lt;h1&gt;&lt;?php itemList(); ?&gt;&lt;/h1&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>