title: Is there a better way than this of creating an PHP array from TSV data?
body: <p>Assuming a tab-separated values (TSV) file with a header line, how would one create a PHP array with the header fields as the key and the data fields as the data?</p>
  <p>Assuming $txtArray contains all the lines in the file,</p>
  <pre><code>$hdrArray = explode( &quot;\t&quot;, $txtArray[0]);
  $i = 0;
  foreach ($hdrArray as $hdr) {
    $heads[$hdr] = '';
    $headerNames[$i++] = $hdr;
  }
  for ($i = 1; $i &lt; (count($txtArray) - 1); $i++ ) {
    $datArray = explode( &quot;\t&quot;, $txtArray[$i]);
    if (count($datArray) &gt; 1) {
      for($j = 0; $j &lt; count($datArray); $j++) {
        $heads[$headerNames[$j]] = $datArray[$j];
      }
    }
    # process the line
  }
  </code></pre>
  <p>I've got $heads containing field_name =&gt; field_data for all the fields in each line of the file. Is there a better way to code this?</p>