title: Building a php multidimensional array from form elements
body: <p>I'm trying to get a multidimensional array to store the info from three separate elements (caption, image, and subfolder).  The three elements are descriptions from photos that individuals may upload onto my site. I've built an initial page that asks the visitor how many photos they intend to upload and from which country(an input type=select).  Based upon their response on that initial form, the second page uses a loop to generate the appropriate number of , , and  elements.  Once the second form is submitted the intention is to upload the info to the server which it does.  The second objective is to store that information inside the multidimensional array such as below in order to upload into a MySQL table.  My dilemma is in getting the image names presumably from ($_FILES['image']['name']) to insert themselves into my multidimensional array.  The captions '' insert themselves into the multidimensional array as does the country name ('name=subfolder']) but not the image names. I'd greatly appreciate anyone willing to help with this.
  Thank you.    </p>
  
  <pre><code>Array ( [caption] =&gt; Array ( [0] =&gt; Nice Simple Picture [1] Another Nice Simple Picture=&gt; )   
  [image] =&gt; [subfolder] =&gt; Array ( [0] =&gt; Italy ) [upload] =&gt; UPLOAD ) 
  </code></pre>
  
  <p>Here's a bit of how I've attempted to go about this..</p>
  
  <pre><code>&lt;?php
  $file=$_FILES['image']['name'];
  
  $expected = array('image','subfolder','caption');
  foreach ($_POST as $key =&gt; $value) {
  if (in_array ($key, $expected)) {
      ${$key} = mysql_real_escape_string($value); }}  
  $sql = "INSERT INTO images (file_name, country, caption)
  VALUES ('$image', '$subfolder', '$caption')"; 
  $result = mysql_query($sql) or die (my_sql_error()); }
  ?&gt;  
  
  &lt;form action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;" id="form1" name="form1" method="POST" 
  enctype="multipart/form-data"&gt; 
  &lt;?php 
  for ($i=0; $i&lt;$imgnumb; $i++) {
  echo "&lt;input type=\"file\" name=\"image[".$i."]\" /&gt;&amp;nbsp;&amp;nbsp;&lt;input type=\"text\"    
  name=\"caption[".$i."]\" /&gt;&lt;br /&gt;"; } 
  ?&gt;
  &lt;br /&gt;
  &lt;input value="&lt;?php echo $file; ?&gt;" name="image" type="hidden" /&gt;
  &lt;input value="&lt;?php echo $location; ?&gt;" name="subfolder" type="hidden" /&gt;
  &lt;input value="&lt;?php echo $_POST['caption']; ?&gt;" name="caption" type="hidden" /&gt;
  &lt;input type="submit" name="upload" id="next" value="UPLOAD" /&gt;
  &lt;/form&gt; 
  </code></pre>