-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 回答