title: How to check if column headers are correct in PHPExcel?
body: <p>I'm using the column header as an array index (row 1), and I don't want to have problems when they start uploading their XLSX file with incorrect column headers (or sometimes missing).</p>
  
  <p>I found this answer really useful. I can make my array indexes as column headers:</p>
  
  <pre><code>for ($row = 2; $row &lt;= $highestRow; $row++){ 
      //  Read a row of data into an array
      $rowData = $sheet-&gt;rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                              NULL,
                                              TRUE,
                                              FALSE);
      $rowData[0] = array_combine($headings[0], $rowData[0]);
  }
  </code></pre>
  
  <blockquote>
    <p>The code above is from <a href="https://stackoverflow.com/a/32526911/8191721">https://stackoverflow.com/a/32526911/8191721</a></p>
  </blockquote>
  
  <p>Here's what I want to happen:</p>
  
  <ol>
  <li>Check first the column headers if they matches with the column
  headers I provided (in array, so I can use <code>in_array</code> in a loop) before
  writing it to the database.</li>
  <li>If the headers are correct, I will now turn the array indexes to column headers (so instead of <code>[0]</code>, it should now return <code>[firstname]</code>) or else throw an error message.</li>
  <li>Write to database where the column names in my database exactly matches the column header names (or if not, at least matches the query) in their XLSX file. (I called them "column mapping").</li>
  </ol>
  
  <p>Long story short, here's my code:</p>
  
  <pre><code>try {
      $inputFileName = $_FILES['file']['tmp_name'];
      $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
      $objReader     = PHPExcel_IOFactory::createReader($inputFileType);
      $objPHPExcel   = $objReader-&gt;load($inputFileName);
  
      //-- Get worksheet dimensions
      $sheet         = $objPHPExcel-&gt;getSheet(0); 
      $highestRow    = $sheet-&gt;getHighestRow(); 
      $highestColumn = $sheet-&gt;getHighestColumn();
      $headings      = $sheet-&gt;rangeToArray('A1:'.$highestColumn.'1', NULL, TRUE, FALSE);
  
      //-- $row = 2 &lt;- skip row 1 since this is our headers
      for($row = 2; $row &lt;= $highestRow; $row++) {
          //-- Read a row of data into an array
          $xlsxRow = $sheet-&gt;rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
  
          //-- Combine to replace indexes with header names
          $xlsxRow[0] = array_combine($headings[0], $xlsxRow[0]);
  
          //-- Got stucked in here..
          /*
            If column header fails, should exit the loop
          */
  
          //-- SQL query follows here..
  }
  </code></pre>
  
  <p>So can someone help me figuring this out?</p>