i have a csv file where the header line is in row 1 not row 0 how can i convert a csv in this situation. I have seen good versions of converting a csv file where the header row is at row[0] as
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
please can someone assist me in the situation where row[1] is the header row.
You can do like this :
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
$i=0;
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if($i==0)
{
$i++;
continue; //SKIP FIRST ROW
}
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments