class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys_values = explode(',',$this->fields[0]);
$content = array();
$keys = $this->escape_string($keys_values);
$i = 1;
while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {
if( $row != null ) { // skip empty lines
$values = explode(',',$row[0]);
if(count($keys) == count($values)){
$arr = array();
$new_values = array();
$new_values = $this->escape_string($values);
for($j=0;$j<count($keys);$j++){
if($keys[$j] != ""){
$arr[$keys[$j]] = $new_values[$j];
}
}
$content[$i]= $arr;
$i++;
}
}
}
fclose($file);
return $content;
}
function escape_string($data){
$result = array();
foreach($data as $row){
$result[] = str_replace('"', '',$row);
}
return $result;
}
}
In this is the code of the library for importing csv files to sql is the key is in the first row. I want to remove the first row keys in csv file and add it to this code.
Fields in first row of csv that I want to be in the already in the code:
$fields = array( '0' => 'model',
'1' => 'serial_num' ,
'2' => 'client_name',
'3' => 'end_user',
'4' => 'trends_warranty_start',
'5' => 'trends_warranty_end',
'6' => 'trends_warranty_period',
'7' => 'client_contract_no',
'8' => 'vendor',
'9' => 'support_coverage',
'10' => 'sla',
'11' => 'service_contract_num',
'12' => 'support_coverage_start',
'13' => 'support_coverage_end',
'14' => 'support_coverage_period',
'15' => 'trends_po',
'16' => 'supplier_so');