Php - Read csv n tsv files to an array, PHP read a files into an array

Read csv n tsv files to an array

function read_file_data_to_array($filepath, $load_keys=false, $filetype) {
 if($filetype=="csv") $separator=",";
  else if($filetype=="tsv") $separator="  ";
  $array = array();
     
 if (!file_exists($filepath)){ return $array; }
  $content = file($filepath);
  for ($x=0; $x < count($content); $x++){
  if (trim($content[$x]) != ''){
  $line = explode("$separator", trim($content[$x]));
  if ($load_keys){
  $key = array_shift($line);
  $array[$key] = $line;
 } else { $array[] = $line; }
 }
}
return $array;
}

/* To Read Tab Separated Files (tsv) */
$tsv_data_array = read_file_data_to_array("filepath", false, "tsv");
 
/* To Read Comma Separated Files (csv) */
$csv_data_array = read_file_data_to_array("filepath", false, "csv");
 
/* Output Array */
print_r($tsv_data_array);
print_r($csv_data_array);

The topic on Php - Read csv n tsv files to an array is posted by - Guru

Hope you have enjoyed, Php - Read csv n tsv files to an arrayThanks for your time

Tech Bluff