-3

テキストファイルへのデータの書き込みを実行するたびに、どうすれば改行できますか?

ファイルは改行なしでレコードを表示します:

countryId:20、productId:332、status:1、opId:188、xId:pdgje9876、countryId:20、productId:334、status:0、opId:188、xId:pfgje3455、

これが私が必要とした出力です:

countryId:20、productId:332、status:1、opId:188、xId:pdgje9876、

countryId:20、productId:334、status:0、opId:188、xId:pfgje3455、

$txt = "log.txt";
$fh = fopen($txt, 'a') or die("can't open file");

foreach($obj as $key => $value) {
    print "$key => $value<br>";

    $stringData = $key.":".$value.", ";
    fwrite($fh, $stringData);

}
fclose($fh);
4

2 に答える 2

2

"\n"すべての行末に追加します。

$txt = "log.txt";
$fh = fopen($txt, 'a') or die("can't open file");

foreach ($objects as $obj) {
  foreach($obj as $key => $value) {
    print "$key => $value<br>";

    $stringData = $key.":".$value.", ";
    fwrite($fh, $stringData);
  }
  fwrite($fh, "\n");
}
fclose($fh);
于 2012-09-24T07:11:17.233 に答える
0

行の最後に「\n」を追加することをお勧めします。

$txt = "log.txt";
$fh = fopen($txt, 'a') or die("can't open file");

foreach($obj as $key => $value) {
    print "$key => $value<br>";
    if ($key == "xld") {
        $stringData = $key.":".$value.",\n"; // Will add new line after xld key
    } else {
        $stringData = $key.":".$value.", ";
    }
    fwrite($fh, $stringData);

}
fclose($fh);
于 2012-09-24T07:11:54.103 に答える