Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

php csv to array with headers

function parse($filecsv){
  $array = $fields = array(); $i = 0;
  $handle = @fopen($filecsv, "r");
  if ($handle) {
      while (($row = fgetcsv($handle, 4096)) !== false) {
          if (empty($fields)) {
              $fields = $row;
              continue;
          }
          foreach ($row as $k=>$value) {
              $array[$i][$fields[$k]] = $value;
          }
          $i++;
      }
      if (!feof($handle)) {
          return false;
      }
      fclose($handle);
  }
  return $array;
}
 
PREVIOUS NEXT
Tagged: #php #csv #array #headers
ADD COMMENT
Topic
Name
5+7 =