php csv to array (csv 转数组)

1. 使用类

 

 注意: user.csv 第一行默认为数组的键, 且第一行不会被打印; (区别于下面的普通函数方法)

 例如:

 

 

nameagegender
zhang23male
li20female

 

结果:

 

 

Php代码   收藏代码
  1. Array  
  2. (  
  3.     [0] => Array  
  4.         (  
  5.             [name] => zhang   
  6.             [age] => 23  
  7.             [gender] => male  
  8.         )  
  9.   
  10.     [1] => Array  
  11.         (  
  12.             [name] => li   
  13.             [age] => 20  
  14.             [gender] => female  
  15.         )  
  16. )  

 

 回到正题:

 

Php代码   收藏代码
  1. <?php   
  2. class csv_to_array{   
  3.     private $counter;   
  4.     private $handler;   
  5.     private $length;   
  6.     private $file;   
  7.     private $seprator;   
  8.     private $csvData = array();   
  9.     function __construct($file = ""$length = 1000, $seprator = ','){   
  10.         $this->counter = 0;    
  11.         $this->length = $length;    
  12.         $this->file = $file;    
  13.         $this->seprator =  $seprator;   
  14.         $this->handler = fopen($this->file, "r");   
  15.     }   
  16.   
  17.     function get_array(){   
  18.         $getCsvArr = array();   
  19.         $csvDataArr = array();   
  20.         while(($data = fgetcsv($this->handler, $this->length, $this->seprator)) != FALSE){   
  21.             $num = count($data);   
  22.             $getCsvArr[$this->counter] = $data;   
  23.             $this->counter++;   
  24.         }   
  25.         if(count($getCsvArr) > 0){   
  26.             $csvDataArr = array_shift($getCsvArr);   
  27.             $counter = 0;   
  28.             foreach($getCsvArr as $csvValue){   
  29.                 $totalRec = count($csvValue);   
  30.                 for($i = 0; $i < $totalRec ; $i++) $this->csvData[$counter][$csvDataArr[$i]] = $csvValue[$i];   
  31.                 $counter++;   
  32.             }   
  33.         }   
  34.         return $this->csvData;   
  35.     }   
  36.   
  37.     function __destruct(){   
  38.         fclose($this->handler);   
  39.     }   
  40. }   
  41.   
  42. // example:   
  43. $csv = new csv_to_array('user.csv');   
  44. echo "<pre>"; print_r($csv->get_array()); echo "</pre>";  

 

2. 使用一般函数

 

 

Php代码   收藏代码
  1. <?  
  2. function csv_to_array($csv)  
  3. {  
  4.     $len = strlen($csv);  
  5.   
  6.   
  7.     $table = array();  
  8.     $cur_row = array();  
  9.     $cur_val = "";  
  10.     $state = "first item";  
  11.   
  12.   
  13.     for ($i = 0; $i < $len$i++)  
  14.     {  
  15.         //sleep(1000);  
  16.         $ch = substr($csv,$i,1);  
  17.         if ($state == "first item")  
  18.         {  
  19.             if ($ch == '"'$state = "we're quoted hea";  
  20.             elseif ($ch == ","//empty  
  21.             {  
  22.                 $cur_row[] = ""//done with first one  
  23.                 $cur_val = "";  
  24.                 $state = "first item";  
  25.             }  
  26.             elseif ($ch == "\n")  
  27.             {  
  28.                 $cur_row[] = $cur_val;  
  29.                 $table[] = $cur_row;  
  30.                 $cur_row = array();  
  31.                 $cur_val = "";  
  32.                 $state = "first item";  
  33.             }  
  34.             elseif ($ch == "\r"$state = "wait for a line feed, if so close out row!";  
  35.             else  
  36.             {  
  37.                 $cur_val .= $ch;  
  38.                 $state = "gather not quote";  
  39.             }  
  40.               
  41.         }  
  42.   
  43.         elseif ($state == "we're quoted hea")  
  44.         {  
  45.             if ($ch == '"'$state = "potential end quote found";  
  46.             else $cur_val .= $ch;  
  47.         }  
  48.         elseif ($state == "potential end quote found")  
  49.         {  
  50.             if ($ch == '"')  
  51.             {  
  52.                 $cur_val .= '"';  
  53.                 $state = "we're quoted hea";  
  54.             }  
  55.             elseif ($ch == ',')  
  56.             {  
  57.                 $cur_row[] = $cur_val;  
  58.                 $cur_val = "";  
  59.                 $state = "first item";  
  60.             }  
  61.             elseif ($ch == "\n")  
  62.             {  
  63.                 $cur_row[] = $cur_val;  
  64.                 $table[] = $cur_row;  
  65.                 $cur_row = array();  
  66.                 $cur_val = "";  
  67.                 $state = "first item";  
  68.             }  
  69.             elseif ($ch == "\r"$state = "wait for a line feed, if so close out row!";  
  70.             else  
  71.             {  
  72.                 $cur_val .= $ch;  
  73.                 $state = "we're quoted hea";  
  74.             }  
  75.   
  76.         }  
  77.         elseif ($state == "wait for a line feed, if so close out row!")  
  78.         {  
  79.             if ($ch == "\n")  
  80.             {  
  81.                 $cur_row[] = $cur_val;  
  82.                 $cur_val = "";  
  83.                 $table[] = $cur_row;  
  84.                 $cur_row = array();  
  85.                 $state = "first item";  
  86.   
  87.             }  
  88.             else  
  89.             {  
  90.                 $cur_row[] = $cur_val;  
  91.                 $table[] = $cur_row;  
  92.                 $cur_row = array();  
  93.                 $cur_val = $ch;  
  94.                 $state = "gather not quote";  
  95.             }     
  96.         }  
  97.   
  98.         elseif ($state == "gather not quote")  
  99.         {  
  100.             if ($ch == ",")  
  101.             {  
  102.                 $cur_row[] = $cur_val;  
  103.                 $cur_val = "";  
  104.                 $state = "first item";  
  105.                   
  106.             }  
  107.             elseif ($ch == "\n")  
  108.             {  
  109.                 $cur_row[] = $cur_val;  
  110.                 $table[] = $cur_row;  
  111.                 $cur_row = array();  
  112.                 $cur_val = "";  
  113.                 $state = "first item";  
  114.             }  
  115.             elseif ($ch == "\r"$state = "wait for a line feed, if so close out row!";  
  116.             else $cur_val .= $ch;  
  117.         }  
  118.   
  119.     }  
  120.   
  121.     return $table;  
  122. }  
  123.   
  124. //pass a csv string, get a php array  
  125. // example:  
  126. $arr = csv_to_array(file_get_contents('user.csv'));  
  127. echo "<pre>"; print_r($arr);   echo "</pre>"  

 

 

 或者

 

 

Php代码   收藏代码
  1. <?  
  2. $arrCSV = array();  
  3. // Open the CSV  
  4. if (($handle = fopen("user.csv""r")) !==FALSE) {  
  5.     // Set the parent array key to 0  
  6.     $key = 0;  
  7.     // While there is data available loop through unlimited times (0) using separator (,)  
  8.     while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {  
  9.        // Count the total keys in each row  
  10.        $c = count($data);  
  11.        //Populate the array  
  12.        for ($x=0;$x<$c;$x++) $arrCSV[$key][$x] = $data[$x];  
  13.        $key++;  
  14.     } // end while  
  15.     // Close the CSV file  
  16.     fclose($handle);  
  17. // end if  
  18. echo "<pre>"; print_r($arrCSV); echo "</pre>";  
  19. ?>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSV to KML换器是一种可以将CSV文件格式换为KML文件格式的工具。CSV文件是一种常见的电子表格文件格式,用于存储以逗号分隔的数据。而KML文件则是一种Keyhole标记语言的文件格式,用于在地理信息系统(GIS)中表示地理数据和位置。 CSV文件通常包含多个列,每列代表不同的数据字段,例如经度、纬度、名称等。通过使用CSV to KML换器,我们可以将这些数据换成KML格式,以便在地图软件或GIS系统中进行显示和分析。 使用CSV to KML换器非常简单。我们只需将待换的CSV文件选择导入到换器中,然后设置一些参数,如经度和纬度的列索引,以及其他需要显示的数据字段。换器会根据这些参数将CSV文件换成对应的KML文件。 换完成后,我们就可以将生成的KML文件导入到支持KML格式的地图软件或GIS系统中进行使用。在地图软件中,KML文件可以显示为点、线、面等不同的地理对象,通过不同的符号标识和颜色可以区分不同的数据。通过这种换,我们可以更直观地展示和分析地理数据,帮助我们更好地理解和利用这些数据。 总之,CSV to KML换器是一种非常实用的工具,通过将CSV格式的数据换为KML格式,我们可以更方便地在地图软件或GIS系统中显示和分析地理数据。这种换可以帮助我们更好地利用地理信息,进一步提升地理数据的可视化和分析能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值