title: PHPExcel upload excel sheet to database using header names
body: <p>I am new to phpexcel so forgive me if this a silly question,
  I have a PHP script that takes an excel file and uploads it to my SQL server. it takes data from different columns and inserts it into two different tables. this works fine as long as the columns in the excel sheet stay the same ex: column 1 = name, column 2 = age and so on. </p>
  
  <pre><code>     $sheet = $objPHPExcel-&gt;getSheet(0); 
       $highestRow = $sheet-&gt;getHighestRow(); 
       $highestColumn = $sheet-&gt;getHighestColumn();
      for ($row = 2; $row &lt;= $highestRow; $row++){
  
          $rowData = $sheet-&gt;rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE, FALSE);
  
              $Name = trim($rowData[0][0]);
              $Age = trim($rowData[0][1]);
              $Address = trim($rowData[0][2]);
      }
  </code></pre>
  
  <p>This is in short what I have. as you can see the $name var is always going to take the value from the first column in the file and so on. my issue is that the file I am uploading will not always be in this order. for example, one week the file will have name in column 1 and age in column 2 but next week age will be in column 1 and name in column 2. </p>
  
  <p>The columns have headings that will always match the data in the row so I would like to use the headers like so.</p>
  
  <pre><code>     $sheet = $objPHPExcel-&gt;getSheet(0); 
       $highestRow = $sheet-&gt;getHighestRow(); 
       $highestColumn = $sheet-&gt;getHighestColumn();
      for ($row = 2; $row &lt;= $highestRow; $row++){
  
          $rowData = $sheet-&gt;rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE, FALSE);
  
              $Name = trim($rowData[0]['NAME']);
              $Age = trim($rowData[0]['AGE']);
              $Address = trim($rowData[0]['ADDRESS']);
  
        sql= "insert into dbo.mainTable (NAME,AGE,ADDRESS) VALUES ('$Name','$Age','$Address')";
  
       $stmt = sqlsrv_query($db,$sql);
      }
  </code></pre>
  
  <p>I think you can do something like this with associative arrays but I just can't work out how to do it with this. I have found some stuff close to what I am asking but when I try any of them it just won't work. if there is a way to do this easily that would be great. This is my last resort as i have searched the internet for nearly a week now so
  thank you in advance. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I added a simple query the same way i am doing it now. the reason i am doing it like this is because the full query is a update query and IF rowcount = 0 then insert plus if the rows change at the moment i just have to go to the list above and change the index. </p>