1

私がやろうとしているのは、BusinessCatalystで生成された製品のCSVを取得することです。しかし、それは私が必要としないものを常にたくさん含んでいます。

fgetcsv()を使用するスクリプトがあります。コードは

<?php
$file_handle = fopen("ProductExport2.csv", "r");
    while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1000000);
            $tableDisplay =  "<tr><td>" . $line_of_text[0] . "</td><td>" . $line_of_text[1] . "</td><td>" . $line_of_text[2] . "</td><td>" . $line_of_text[4] . "</td><td>" . $line_of_text[6] . "</td><td>" .  $line_of_text[49] . "</td></tr>";
            echo $tableDisplay;
    }
fclose($file_handle);
?>

これは、必要なデータを表示するだけです。

しかし、私が今やりたいのは、これから新しいCSVファイルを書くことです。fwrite()を使用すると、データの最初のエントリのみが書き込まれます。

何か案は?

4

2 に答える 2

0

このようなもの。

<?php
    $file_handle = fopen("ProductExport2.csv", "r");
    $data = '';
    while (!feof($file_handle) ) {
        $line_of_text = fgetcsv($file_handle, 1000000);
        $data .=  $line_of_text[0] . "," . $line_of_text[1] . "," . $line_of_text[2] . "," . $line_of_text[4] . "," . $line_of_text[6] . "," .  $line_of_text[49] . "\n\r";
    }
    fclose($file_handle);

    $new_file_handle = fopen("ProductExport2_reduced.csv", "w");
    fwrite($new_file_handle, $data);
?>
于 2012-02-06T10:35:13.873 に答える
0

これは、既に持っているコードと非常によく似ています。

<?php
$file_handle = fopen("ProductExport2.csv", "w");
    // Iterate through the data entries
    foreach($my_data as $entry) {
      // Write the "columns" of each entry as a comma-separated string to the file
      fputcsv($file_handle, $entry);
    }
fclose($file_handle);

$my_data2 次元配列でなければなりません。

既存のファイルを読み取り、その内容を処理して別のファイルに書き込む場合は、次のようにします。

<?php
$file_handle = fopen("ProductExport2.csv", "r");
$file_handle_output = fopen("ProductExport2_new.csv", "w");
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1000000);
    // Change $line_of_text elements here
    // for example $line_of_text[1] += 100;
    // ...
    fputcsv($file_handle_output, $line_of_text);    
}
fclose($file_handle);
fclose($file_handle_output);
?>
于 2012-02-06T10:35:47.807 に答える