I have a PHP procedure that reads a CSV file that has been uploaded to my site.
The number of fields in the file may differ from download to download.
I want to be able to set the CSV file size (number of fields) and then save its contents in an array.
This is what I still have:
$file = $_FILES[csv1][tmp_name];
$handle = fopen($file,"r");
$dataline = fgetcsv($handle,1000,",","'");
$fieldnum = count($dataline);
$recordloop = 0;
while ($dataline)
{
for ($fieldloop=0; $fieldloop <$fieldnum; $fieldloop++)
{
$dataarray[]=array($dataline[$fieldloop]);
}
$data = fgetcsv($handle,1000,",","'");
$recordloop++;
}
If, for example, there are 30 fields and 200 records, I try to get an array of structure $dataarray[200][30].
How to get data in an 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 exactly what I wanted
$file = $_FILES[csv1][tmp_name];
$handle = fopen($file,"r");
$matrix = array();
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$matrix[] = $row;
}