I have a PHP routine that reads a .csv file that has been uploaded to my site.
The number of fields in the file may be different from upload to upload.
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.
This is what I have so far:
//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 <$fieldnum; $fieldloop++)
{
//this is the bit that is wrong
$dataarray[]=array($dataline[$fieldloop]);
}
$data = fgetcsv($handle,1000,",","'");
$recordloop++;
}
If for example, there are 30 fields and 200 records I am trying to get the array to be of the structure $dataarray[200][30]
.
How do I get the data into the array of this structure?
Example .csv:
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
ANSWER - did just what I wanted
$file = $_FILES[csv1][tmp_name];
$handle = fopen($file,"r");
$matrix = array();
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$matrix[] = $row;
}