title: PHP Create Multidimensional Array Dynamically In Form & Keep Form Values After Posting
body: <p>I have searched for a few weeks now and am not able to find a solution to my problem. I've tried to understand what is written in the PHP Manual, but the examples don't match my issue. I've come close, specifically on this site, including a question similar to this one, but for a table display, but the questions and examples are enough different than my problem that I am not able to interpret those answers and modify them to my code. Or they are so advanced as to be over my head.</p>
  
  <p>I need help, please...</p>
  
  <p>My problem is trying to create a set of dynamic text boxes in a form, the number of sets based on user input. I then want to be able to keep those values entered on the form if there is a POST error. I can do this with all other fields on the form, but not the multidimensional array.</p>
  
  <p>The set of text boxes are:</p>
  
  <p>gFirstName, gLastName, gEmail</p>
  
  <p>If a user enters a value of "3", then I need 3 rows of text boxes for gFirstName, gLastName, gEmail to display on the form.</p>
  
  <p>If a user enters a value of "5", then I need 5 rows of text boxes for gFirstName, gLastName,gEmail to display on the form.  </p>
  
  <p>I can do a very simple echo display of the values, which work, but when I try and reproduce those values in the textbox form, I display the associative key or the numeric array value, or even worse, the word "array".</p>
  
  <p>In addition, I try to check if the array is set or empty, so as not to get the dreaded offset or undefined error, yet I still get one of those, depending on how I modify the statement.</p>
  
  <p>sigh....</p>
  
  <p>Here is the multidimensional array display that does work:</p>
  
  <pre><code>$guestData = array
  (
  "gFirstName" =&gt; array("cindy","cheryl","john"),
  "gLastName" =&gt; array("johnson","smith","jones"),      
  "gEmail" =&gt; array("cindy@hotmail.com","cheryl@gmail.com","john@hotmail.com"),
  ); 
  
  foreach($guestData as $x =&gt; $gkey) 
  foreach($gkey as $key =&gt; $x_value) 
  {
  if ($x_value&lt;&gt; ' ') 
  {
      echo "Guest Data values are: x gkey is:  " . $x .  "     and key is:  " .  $key   . "      and x_value is: ".  $x_value ." &lt;br&gt; ";
  }
  
  next($guestData);
  }
  </code></pre>
  
  <p>Here is the form code that I am trying to get to work:</p>
  
  <pre><code>if(is_array($guestData) and isset($guestData)) {
  $keys = array_keys($guestData);         
  $count = count($guestData[$keys[0]]);   
  for($i=0;$i&lt;$count;$i++){                                           
  $gnum = $key + 1;
  
  echo "&lt;label class=\"inq5a\"&gt;Guest $gnum First Name:  &lt;span class=\"error\"&gt;*&lt;/span&gt; &lt;/label&gt; ";                                
  echo "&lt;input class=\"textreg6\" type=\"text\" name=\'gFirstName\'  value={$guestData['gFirstName'][$i]}&lt;/input&gt;" ;
  
  echo "&lt;label class=\"inq5b\"&gt;Last Name :  &lt;span class=\"error\"&gt;*&lt;/span&gt; &lt;/label&gt;";     
  echo "&lt;input class=\"textreg6c\" type=\"text\" name=\'gLastName\' value={$guestData['gLastName'][$i]}   &lt;/input&gt;" ;
  
  echo "&lt;label class=\"inq5b\"&gt;Email :  &lt;span class=\"error\"&gt;*&lt;/span&gt; &lt;/label&gt; ";
  echo "&lt;input class=\"textreg6c\" type=\"text\" name=\'gEmail\' value={$guestData['gEmail'][$i]}&lt;/input&gt;" ;
  
  }   
  next($guestData);
  
  } 
  </code></pre>
  
  <p>The result of the simple display is:</p>
  
  <pre><code>Simple Display: x gkey is: gFirstName and key is: 0 and x_value is: cindy
  Simple Display: x gkey is: gFirstName and key is: 1 and x_value is: cheryl
  Simple Display: x gkey is: gFirstName and key is: 2 and x_value is: john
  Simple Display: x gkey is: gLastName and key is: 0 and x_value is: johnson
  Simple Display: x gkey is: gLastName and key is: 1 and x_value is: smith
  Simple Display: x gkey is: gLastName and key is: 2 and x_value is: jones
  Simple Display: x gkey is: gEmail and key is: 0 and x_value is: cindy@hotmail.com
  Simple Display: x gkey is: gEmail and key is: 1 and x_value is: cheryl@gmail.com
  Simple Display: x gkey is: gEmail and key is: 2 and x_value is: john@hotmail.com 
  </code></pre>
  
  <p>The result of the form value only gives me the last value (key 2) and is shown below:</p>
  
  <pre><code>Guest 3 First Name: * john       Last Name : *  john      Email : * john
  Guest 3 First Name: * jones      Last Name : *  jones     Email : * jones
  Guest 3 First Name: * john@hotmail.com  Last Name : * john@hotmail.com  Email : john@hotmail.com
  </code></pre>
  
  <p>It seems to be taking the text value from the Input statement at the beginning of the line.</p>
  
  <p>So to summarize the problem:</p>
  
  <p>I am not able to correctly populate the text form with an existing array value.
  The array is only displaying the values from the final "row" of the array group, not all the groups as it should.</p>
  
  <p>Thank you for whatever help you can give me. </p>