-1

我从 html 获取名称和数据。我想将其写入 CSV。

所以我得到了这个 CSV。

 ColA    ColB         ColC   ColD   ColE   ColF   ColG
 name1   surname1     data1  data2   data3 data4 data5
 name2   surname2     data6  data7   data8 data9 data10
 name3   surname3     data11 data12  data13 data14 data15
 name..n surname..n data..n data..n  data..n data..n data..n

但我想要 ColA 中的姓名和姓氏。

     ColA           ColB   ColC   ColD   ColE   ColF  
 name1 surname1     data1  data2   data3 data4 data5
 name2 surname2     data6  data7   data8 data9 data10
 name3 surname3     data11 data12  data13 data14 data15
 name..n surname..n data..n data..n  data..n data..n data..n

我认为是因为 "space" 的名字和姓氏(有些有中间名)但 Data 没有空格。我想在 ColA 中写名字和姓氏。请帮助我。TT

 $fp = fopen("data.csv","w");
   $string = '';
    for ($i=0; $i<$count_name; $i++)
{
while ( $iterate <= 5 and $j < $count_data )
{
    $string = $string.$temp_data[$j]." ";
    $j++;
    $iterate++;
}
$csv_string = $temp_name[$i]." ".$string;
//echo $temp_name[$i];
$array = explode(' ',$csv_string);
//var_dump($array);
fputcsv($fp,$array);
//$string = "";
$iterate = 1;
$string = "";

}
4

1 回答 1

0

如果我理解正确,你应该试试这个:

$fp = fopen('data.csv', 'w');
for(/* Each line of data you have */){
    $colA = $name ." ".$surname; // Concatenate name and surname that you have received from your html
    $colB = $data1; // Data1 from html
    $colC = $data2; // Data2 from html
    fputcsv($fp, array($colA,$colB,$colC)); // A new line on your csv
}
fclose($fp);
于 2013-04-13T18:57:07.503 回答