2014-11-08 98 views
0

我想將一個CSV文件轉換成一個PHP數組,它以某種方式將所有的東西加入到單個字符串中。我希望用*號開始一個新行,以「,,」結尾,並用「,」分開。轉換CSV數組

下面是CSV的部分:

*,Alerts,Alert,Type,Text,,,,,,phr_ccr,alert,Type,, 
*,Alerts,Alert,Type,Code,Value,,,,,phr_ccr,alert,Type,, 
*,Alerts,Alert,Type,Code,CodingSystem,,Text,,,phr_ccr,alert,Type,, 
*,Alerts,Alert,Agent,Products,Product,Description,Code,Value,,phr_ccr,alert,Product_Name_CD,, 
*,Alerts,Alert,Agent,Products,Product,Description,Code,CodingSystem,,phr_ccr,alert,Product_Name_CDS,, 
*,Alerts,Alert,Agent,Products,Product,Description,ProductName,*,,phr_ccr,alert,Product_Name,, 
+2

是直線尚未被EOL字符分隔? – 2014-11-08 16:03:55

+2

fgetcsv會處理這個很好 – Ghost 2014-11-08 16:06:18

回答

0

試試這個代碼,如果它有助於:

$csv_name;#name of ur csv file 
    $raw_data=array(); 
    $count=0; 
    $csv = fopen($csv_name[$count_list], 'r'); 
    while (($csv_data = fgetcsv($csv, ",")) !== FALSE) 
    { 
     #your csv column names: 
     $alert=$csv_data[0]; 
     $alerttype=$csv_data[1]; 
     #and so on 
     $raw_data[$count]=$alert."#".$alerttype."#so on according to ur need"; 
     $count++; 
    }