PHP 解析CSV

<?php
    function parse_csv_file($file, $columnheadings = false, $delimiter = ',', $enclosure = """) {

       $row = 1;
       $rows = array();
       $handle = fopen($file, 'r');

       while (($data = fgetcsv($handle, 1000, $delimiter, $enclosure )) !== FALSE) {

           if (!($columnheadings == false) && ($row == 1)) {
               $headingTexts = $data;
           } elseif (!($columnheadings == false)) {
               foreach ($data as $key => $value) {
                   unset($data[$key]);
                   $data[$headingTexts[$key]] = $value;
               }
               $rows[] = $data;
           } else {
               $rows[] = $data;
           }
           $row++;
       }

       fclose($handle);
       return $rows;
    }
?>

未经允许不得转载:SuperMan's blog » PHP 解析CSV

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址