title: Reading .csv into a PHP array
body: <p>I have a PHP routine that reads a .csv file that has been uploaded to my site. </p>
  
  <p>The number of fields in the file may be different from upload to upload. </p>
  
  <p>I want to be able to establish the size of the .csv file (the number of fields) and then store its contents in an array. </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>//get the csv file 
  $file = $_FILES[csv1][tmp_name]; 
  $handle = fopen($file,"r"); 
  
  //loop through the csv file and insert into array
  $dataline = fgetcsv($handle,1000,",","'");
  $fieldnum = count($dataline);
  
  $recordloop = 0;
  
  while ($dataline) 
  { 
  
      for ($fieldloop=0; $fieldloop &lt;$fieldnum; $fieldloop++)
      {
          //this is the bit that is wrong
          $dataarray[]=array($dataline[$fieldloop]);
      }
  
      $data = fgetcsv($handle,1000,",","'");
      $recordloop++;
  }
  </code></pre>
  
  <p>If for example, there are 30 fields and 200 records I am trying to get the array to be of the structure <code>$dataarray[200][30]</code>. </p>
  
  <p>How do I get the data into the array of this structure? </p>
  
  <p>Example .csv:</p>
  
  <pre><code>Row 1 will be field headings
  Name, Gender, Ethnicity, DOB, etc .... the number of fields is not fixed - it could be from 30 to 40 fields in this row
  
  Rows 2 onwards will be the records
  John, M, British, 10/04/49, etc
  Simon, M, British, 04/03/65, etc
  </code></pre>
  
  <p>ANSWER - did just what I wanted  </p>
  
  <pre><code>$file = $_FILES[csv1][tmp_name]; 
  $handle = fopen($file,"r"); 
  
  $matrix = array();
  while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
  {
  $matrix[] = $row;
  }
  </code></pre>